Usually, the only way to check the success of the allocator functions would be to check for the returned pointer by the realloc()
call against a NULL
pointer. If the returned pointer is not NULL
, you can be assured that the call is success.
Following the lines, the approach like
pointer = realloc (pointer, newValue);
is problematic, as in case of realloc()
failure, quoting from C11
, chapter §7.22.3.5,
[....] If memory for the new object cannot be
allocated, the old object is not deallocated and its value is unchanged.
and
The realloc
function returns a pointer to the new object (which may have the same
value as a pointer to the old object), or a null pointer if the new object could not be
allocated.
Thus, the returned null-pointer will overwrite the previous valid memory address and you'll lose the access and leak memory. The safe course of action is to
type tempPOinter = NULL;
tempPOinter = realloc(oldPointer, newSize);
if (tempPOinter) {
oldPointer = tempPOinter;
}
else {
printf("Failure in realloc!!");
free(oldPointer);
return -1;
}