Re:Once upon a time, memory allocators made sense
by cperciva on Monday January 18, 2016 @02:38PM
(#10926384)
realloc can change the size of a memory allocation and return the same pointer.
[ Parent
| on HN ]
Re:Once upon a time, memory allocators made sense
by antirez on Tuesday January 19, 2016 @03:37AM
(#10929393)
What 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. */
}