c/memory/realloc

Reallocates the given area of memory. If is not NULL, it must be previously allocated by malloc, calloc or  and not yet freed with a call to free or. Otherwise, the results are undefined.

The reallocation is done by either: @a@ expanding or contracting the existing area pointed to by, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the new part of the array are undefined. @b@ allocating a new memory block of size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block.

If there is not enough memory, the old memory block is not freed and null pointer is returned.

If is null, the behavior is the same as calling.

Otherwise,

Return value
On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with free or. The original pointer is invalidated and any access to it is undefined behavior (even if reallocation was in-place).

On failure, returns a null pointer. The original pointer remains valid and may need to be deallocated with free or.