Difference between revisions of "cpp/memory/c/realloc"
(note ub) |
(mention that optional is not relocatable) |
||
Line 22: | Line 22: | ||
Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that | Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that | ||
* does not have to register with an observer that maintains a pointer to this object (e.g. nodes of a list or a tree) | * does not have to register with an observer that maintains a pointer to this object (e.g. nodes of a list or a tree) | ||
− | * has no members of pointer type that point to members of the same object | + | * has no members of pointer type that point to members of the same object (like {{lc|std::experimental::optional}}) |
Objects of such type can be accessed after their storage is reallocated even if their copy constructors are not trivial. | Objects of such type can be accessed after their storage is reallocated even if their copy constructors are not trivial. |
Revision as of 07:00, 16 May 2015
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:
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. 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).
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
.
Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that
- does not have to register with an observer that maintains a pointer to this object (e.g. nodes of a list or a tree)
- has no members of pointer type that point to members of the same object (like std::experimental::optional)
Objects of such type can be accessed after their storage is reallocated even if their copy constructors are not trivial.
Parameters
ptr | - | pointer to the memory area to be reallocated |
new_size | - | new size of the array |
Return value
On success, returns a pointer to the beginning of newly allocated memory. The returned pointer must be deallocated with std::free(), the original pointer ptr
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 ptr
remains valid and may need to be deallocated with std::free()
Example
#include <cstdlib> #include <new> #include <cassert> class MallocDynamicBuffer { char* p; public: explicit MallocDynamicBuffer(std::size_t initial = 0) : p(nullptr) { resize(initial); } ~MallocDynamicBuffer() { std::free(p); } void resize(std::size_t newSize) { if(newSize == 0) { // this check is not strictly needed, std::free(p); // but zero-size realloc is deprecated in C p = nullptr; } else { if(void* mem = std::realloc(p, newSize)) p = static_cast<char*>(mem); else throw std::bad_alloc(); } } char& operator[](size_t n) { return p[n]; } char operator[](size_t n) const { return p[n]; } }; int main() { MallocDynamicBuffer buf1(1024); buf1[5] = 'f'; buf1.resize(10); // shrink assert(buf1[5] == 'f'); buf1.resize(1024); // grow assert(buf1[5] == 'f'); }
See also
C documentation for realloc
|