3 ms·
realloc can change the size of a memory allocation and return the same pointer.
by cperciva 11y ago
realloc can change the size of a memory allocation and return the same pointer.
- antirez 11y agoWhat I mean is that, when you say: > Unfortunately, some people are not sane, and have decided that it's equally valid for realloc(p, 0) to return NULL meaning-failure and not free p. What you want is the following semantics: char *my_old_vector = ... ; /* We created / allocated in some way. */ add_important_stuff_to(my_old_vector); char *new_vector = realloc(my_old_vector,size+100); if (new_vector == NULL) { /* I still have the old vector and can recover. */ }