3 ms·
You can make my_realloc() simpler: void *my_realloc(void *ptr, size_t size) { return size ? realloc(ptr, size) : (free(ptr), 0); } Accordi
by klodolph 11y ago
You can make my_realloc() simpler:
void *my_realloc(void *ptr, size_t size)
{
return size ? realloc(ptr, size) : (free(ptr), 0);
}
According to the C standard, realloc(NULL, size) is guaranteed to be equivalent to malloc(size). From n1548 7.22.3.5 para 3:
> If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.
Also, free(0) is well-defined. From n1548 7.22.3.3 para 2:
> If ptr is a null pointer, no action occurs.