Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED C: what happens with memory if stdlib realloc fails?
#1
Reference: http://www.cplusplus.com/ref/cstdlib/realloc.html

If realloc fails a null pointer will be returned, but is the original memory chunk freed?
In other words is it safe to do this:

Code:
int* pointer; pointer=alloc(sizeof(int)*10); pointer=realloc(sizeof(int)*100); free(pointer);
Or do I have to do this to be sure to avoid memory leaks?:
Code:
int* pointer;
int* ptmp;
pointer=alloc(sizeof(int)*10);
ptmp=realloc(sizeof(int)*100);
if (ptmp!=0) {
    pointer=ptmp;
}
else {
    free(pointer); //<------ free the original memory
    //Other error handling
}



EDIT:
see;http://www.c-faq.com/malloc/realloc.html
/post]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)