Difference between revisions of "cpp/memory/c/realloc"
From cppreference.com
m (Text replace - "/sidebar" to "/navbar") |
m (Use new list syntax) |
||
Line 8: | Line 8: | ||
The reallocation is done by either: | The reallocation is done by either: | ||
− | + | @a@ expanding the existing area pointed to by {{tt|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 {{tt|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 there is not enough memory, the old memory block is not freed and null-pointer is returned. |
Revision as of 12:17, 30 August 2012
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 malloc()
, calloc()
or realloc()
and not yet freed with free()
, otherwise, the results are undefined.
The reallocation is done by either:
a) expanding 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.
Contents |
Parameters
ptr | - | pointer to the memory area to be reallocated |
new_size | - | new size of the array |
Return value
Pointer to the beginning of newly allocated memory or NULL if error has occurred. The pointer must be deallocated with free()
.
Example
This section is incomplete Reason: no example |
See also
C documentation for realloc
|