Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/c/realloc"

From cppreference.com
< cpp‎ | memory‎ | c
(typo)
m (Shorten template names. Use {{lc}} where appropriate.)
Line 17: Line 17:
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | ptr | pointer to the memory area to be reallocated}}
+
{{par | ptr | pointer to the memory area to be reallocated}}
{{param list item | new_size | new size of the array}}
+
{{par | new_size | new size of the array}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
Pointer to the beginning of newly allocated memory or {{c|NULL}} if error has occurred. The pointer must be deallocated with {{rlpf|free}}.
+
Pointer to the beginning of newly allocated memory or {{lc|NULL}} if error has occurred. The pointer must be deallocated with {{rlpf|free}}.
  
 
===Example===
 
===Example===
Line 32: Line 32:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list see c | c/memory/realloc}}
+
{{dsc see c | c/memory/realloc}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/memory/c/realloc]]
 
[[de:cpp/memory/c/realloc]]

Revision as of 19:13, 31 May 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
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 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.

Contents

Notes

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

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

See also

C documentation for realloc