|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <cstdlib> | ||
void* realloc(void* ptr,std::size_t new_size); | ||
Reallocates the given area of memory (implicitly creating objects in the destination area). It must be previously allocated bystd::malloc,std::calloc orstd::realloc and not yet freed withstd::free, otherwise, the results are undefined.
The reallocation is done by either:
If there is not enough memory, the old memory block is not freed and null pointer is returned.
Ifptr is a null pointer, the behavior is the same as callingstd::malloc(new_size).
Ifnew_size 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.Such usage is deprecated (viaC DR 400).(since C++20)
The following functions are required to be thread-safe:
Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation callhappens-before the next allocation (if any) in this order. | (since C++11) |
Contents |
| ptr | - | pointer to the memory area to be reallocated |
| new_size | - | new size of the array |
On success, returns a pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated withstd::free orstd::realloc. The original pointerptr is invalidated and any access to it isundefined behavior (even if reallocation was in-place).
On failure, returns a null pointer. The original pointerptr remains valid and may need to be deallocated withstd::free.
Because reallocation may involve bytewise copying (regardless of whether it expands or contracts the area), it is necessary (but not sufficient) for those objects to be ofTriviallyCopyable type.
Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that does not have:
Objects of such type can be accessed after their storage is reallocated even if their copy constructors are not trivial.
#include <cassert>#include <cstdlib>#include <new> 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);elsethrowstd::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);// shrinkassert(buf1[5]=='f'); buf1.resize(1024);// growassert(buf1[5]=='f');}
C documentation forrealloc |