cpp/memory/c/realloc

Reallocates the given area of memory (implicitly creating objects in the destination area). It must be previously allocated by std, std or and not yet freed with std, 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 a null pointer, the behavior is the same as calling std.

If is zero, the behavior is implementation defined: null pointer may be returned (in which case the old memory block may or may not be freed) or some non-null pointer may be returned that may not be used to access storage.

Return value
On success, returns a pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with std 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 std.