First of all,
void **ptr = realloc(ptr, nsize);
is wrong, as you're using ptr
uninitialized (that is being defined here), and as per realloc()
function description from C11
, chapter §7.22.3.5
If ptr
is a null pointer, the realloc
function behaves like the malloc
function for the
specified size. Otherwise, if ptr
does not match a pointer earlier returned by a memory
management function, or if the space has been deallocated by a call to the free or
realloc
function, the behavior is undefined. [...]
So, your code invokes undefined behavior, as you're passing a pointer which contains an indeterminate value.
However, considering your case to be something like
void **ptr = malloc(size);
assert (ptr);
ptr = realloc(ptr, nsize);
it is a very poor usage, in case realloc
fails (where it does not alter the original memory and return NULL
), you'll end up losing the actual pointer, too. Use an intermediate variable to store validate the returned pointer, and then assign it back to the original variable, as needed.
That said, re-check the quote (emphasis mine)
The realloc()
function changes the size of the memory block pointed to by ptr
to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized.
So, to answer
If the original size is size
, does that mean ptr + nsize + 1
still contains allocated entries?
No, we can;t say. After the successful realloc()
call, we are only allowed to access up to ptr + nsize - 1
. Trying to read/write ptr + nsize
and onwards is undefined, as that memory location does not belong to your process anymore and that memory location is "invalid".
You should not have any need to bother about the content beyond ptr + nsize - 1
, anyways.