##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::realloc

From cppreference.com
< cpp‎ | memory‎ | c

Defined in header `<cstdlib>` |  |
void* realloc( void* ptr, std::size_t new_size ); |  |

Reallocates the given area of memory. It must be previously allocated by std::malloc(), std::calloc() or `std::realloc()` and not yet freed with std::free(), otherwise, the results are undefined.

The reallocation is done by either:

a) expanding or contracting the existing area pointed to by `ptr`, 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 `new_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 `ptr` is NULL, the behavior is the same as calling std::malloc(`new_size`).

If `new_size` is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage).

### [edit] Notes

Because reallocation may involve bytewise copying (regardless of whether it's to expand or to contract), only the objects of `TriviallyCopyable` types are safe to access in the preserved part of the memory block after a call to `realloc`.

### [edit] Parameters

ptr | - | pointer to the memory area to be reallocated
new_size | - | new size of the array

### [edit] Return value

Pointer to the beginning of newly allocated memory or NULL if error has occurred. The pointer must be deallocated with std::free().

### [edit] Example

| This section is incomplete Reason: no example

### [edit] See also

C documentation for realloc

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/c/realloc&oldid=67167"
Category:

- Todo no example