 st8b7c:f320:99b9:690f:4595:cd17:293a:c069realloc    Defined in header <cstdlib>         void* realloc( void* ptr, st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t new_size );
       Reallocates the given area of memory. It must be previously allocated by st8b7c:f320:99b9:690f:4595:cd17:293a:c069malloc(), st8b7c:f320:99b9:690f:4595:cd17:293a:c069calloc() or st8b7c:f320:99b9:690f:4595:cd17:293a:c069realloc() and not yet freed with st8b7c:f320:99b9:690f:4595:cd17:293a:c069free(), 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 a null pointer, the behavior is the same as calling st8b7c:f320:99b9:690f:4595:cd17:293a:c069malloc(new_size).
If new_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 (via C DR 400). (since C++20).
 
 The following functions are required to be thread-safe:

 The library versions of operator new and operator delete 
 User replacement versions of global operator new and operator delete 
 st8b7c:f320:99b9:690f:4595:cd17:293a:c069calloc, st8b7c:f320:99b9:690f:4595:cd17:293a:c069malloc, st8b7c:f320:99b9:690f:4595:cd17:293a:c069realloc, st8b7c:f320:99b9:690f:4595:cd17:293a:c069aligned_alloc (since C++17), st8b7c:f320:99b9:690f:4595:cd17:293a:c069free 
 Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation call happens-before the next allocation (if any) in this order.
 (since C++11)
   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. To avoid a memory leak, the returned pointer must be deallocated with st8b7c:f320:99b9:690f:4595:cd17:293a:c069free(), 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 st8b7c:f320:99b9:690f:4595:cd17:293a:c069free().
 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.
Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that doesn't have:

 external references (e.g. nodes of a list or a tree that holds reference to another element), and 
 internal references (e.g. member pointer which might hold the address of another member). 
 Objects of such type can be accessed after their storage is reallocated even if their copy constructors are not trivial.
 Example  #include <cstdlib>
#include <new>
#include <cassert>
 
class MallocDynamicBuffer
{
    char* p;
public:
    explicit MallocDynamicBuffer(st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t initial = 0) : p(nullptr) {
       resize(initial);
    }
    ~MallocDynamicBuffer() { st8b7c:f320:99b9:690f:4595:cd17:293a:c069free(p); }
    void resize(st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t newSize) {
        if(newSize == 0) { // this check is not strictly needed, 
            st8b7c:f320:99b9:690f:4595:cd17:293a:c069free(p);  // but zero-size realloc is deprecated in C
            p = nullptr;
        } else {
            if(void* mem = st8b7c:f320:99b9:690f:4595:cd17:293a:c069realloc(p, newSize))
                p = static_cast<char*>(mem);
            else
                throw st8b7c:f320:99b9:690f:4595:cd17:293a:c069bad_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 
  

    
{'path': 'public/docs/cpp/memory/c/realloc.html', 'source': 'DevDocs.IO'}