Consider this code:
main() {
float *ptr = NULL;
while(true) {
ptr = (float *)realloc(ptr, 0*sizeof(float));
fprintf(stdout,"Errno: %d, Ptr value: %d\n",errno, (int)ptr);
}
}
What is odd is that errno is never set (at least for me) but the call alternatively returns NULL and a pointer value. My thinking is that 0 allocations can return an error of a sort, but not one severe enough to set errno. Or the code with realloc is problematic. I am not sure.
I sort of would not care, but this is causing me a (0 byte) memory leak.
The 'Realloc Failure' question is not quite the same as it largely assumes that a NULL return from realloc() is an error. This is not the case in this situation. This is mostly about the different behavior of realloc() when a zero size is passed to it.