Consider the following:
int* pi = malloc(sizeof(int));
//-- stuff goes here --
pi = realloc(2*sizeof(int));
If realloc fails it will return a null pointer but will it also free
the memory that pi previously pointed to?
Or do I have to realloc to a temporary variable so I still have a
pointer to the original data?
Like this:
int* pi = malloc(sizeof(int));
//-- stuff goes here --
int* tmp = realloc(2*sizeof(int));
if (tmp != 0) {
pi=tmp;
}
else {
// realloc failed, but I still have the memory previously allocated
at *pi so I can free it if I need to.
}