Your original condition isn't quite correct: actually the pointer passed to realloc
... must be previously allocated by
malloc()
,calloc()
orrealloc()
and not yet freed with a call to free or realloc
[OR] If ptr is NULL, the behavior is the same as calling
malloc(new_size)
.
The realloc
function only works with things that were originally created with a small group of allocation functions (such as malloc
, calloc
, or realloc
itself), or the null pointer. Since string
is none of those things, your code is not well-defined.