char *oldPointer, *newPointer;
oldPointer = (char*)malloc(1000);
newPointer = (char*)realloc(oldPointer, 2000)
Can oldPointer
be used now?
What happens now?
oldPointer = newPointer;
What happens if free(newPointer)
?
char *oldPointer, *newPointer;
oldPointer = (char*)malloc(1000);
newPointer = (char*)realloc(oldPointer, 2000)
Can oldPointer
be used now?
What happens now?
oldPointer = newPointer;
What happens if free(newPointer)
?
What happens when 2 pointers point to same area and 1 of them is freed?
oldPointer = (char*)malloc(1000);
newPointer = (char*)realloc(oldPointer, 2000)
oldPointer and newPointer might not point to the same area. This is what realloc()
does. The new pointer might be different if the memory manager decides to do so (for instance if the old area does not have enough space to cover the new required space). Assuming they do point to the same area induces an undefined behavior (UB - may or not may not work as you expect, leading to a difficult debugging).
1 of them is freed
After
oldPointer = (char*)malloc(1000);
newPointer = (char*)realloc(oldPointer, 2000)
You cannot use oldPointer
after that realloc
. Only newPointer
exists. If you free oldPointer
and the address changed, UB (crash likely). If you free oldPointer
and it was the same address as newPointer
, this is the same as freeing newPointer
, of course.
Anyway, the rule is simple: do not use oldPointer
after it's be realloc'ed. Unless
oldPointer = malloc(1000);
oldPointer = realloc(oldPointer, 2000);
^^^
you use the same pointer. But is not advised as if the realloc
fails you have no trace of the previously allocated address (unless you saved it) ; it's a leak.
And don't cast malloc / realloc.
You cannot safely use either pointer before checking what happened after realloc().
Possibility 1:
realloc failed, then newpointer will be NULL and cannot be used, but oldPointer can.
Possibility 2:
realloc succeded and did not have to relocate the old malloc'd memory. In that case you can use both pointers. (They hold the same address value)
Possibility 3:
realloc succeded but had to allocate memory at a different place and free the old block of memory. Now oldPointer will still point at the old memory address, which is not valid anymore. This is known as a dangling pointer.
However, newPointer is valid and can be used.
Can oldPointer be used now?
When you say "be used" I assume that you mean "can the pointer be dereferenced". In other words - is it ok to do *oldPointer
or oldPointer[200]
or something similar.
The answer is: It depends on the value of newPointer
.
The function realloc
will return a pointer to the memory after reallocation unless an allocation error occur (e.g. out of memory). If an error occur realloc
returns NULL.
Therefore the correct way of using realloc
is to save the return value in another pointer and then check for NULL. Like
oldPointer = (char*)malloc(1000); // BTW: dont use cast
newPointer = (char*)realloc(oldPointer, 2000); // BTW: dont use cast
if (newPointer == NULL)
{
// realloc failed....
// the value of oldPointer is still valid
// It is ok to dereference oldPointer, e.g. using oldPointer[10]
// Here you will normally have some error handling
}
else
{
// realloc success
// the value of oldPointer shall be considered invalid.
// the value of oldPointer may NOT be dereferenced anymore.
// also notice that you may NOT call free(oldPointer).
// Normally we save the value of newPointer into oldPointer
// so that the value of oldPointer becomes valid and usable again
oldPointer = newPointer;
}
What happens if free(newPointer)?
If newPointer
points to the same memory as oldPointer
it will no longer be allowed to dereference any of the pointers. Example:
oldPointer = newPointer;
free(newPointer);
oldPointer[9] = something; // Illegal !!
newPointer[9] = something; // Illegal !!
Per the the standard:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.