Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory‎ | c
(Improved example. It's ok to free null.)
m (fmt)
 
(20 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|realloc}}
 
{{cpp/title|realloc}}
 
{{cpp/memory/c/navbar}}
 
{{cpp/memory/c/navbar}}
{{ddcl | header=cstdlib |
+
{{ddcl|header=cstdlib|
 
void* realloc( void* ptr, std::size_t new_size );
 
void* realloc( void* ptr, std::size_t new_size );
 
}}
 
}}
  
Reallocates the given area of memory. It must be previously allocated by {{lc|std::malloc()}}, {{lc|std::calloc()}} or {{tt|std::realloc()}} and not yet freed with {{lc|std::free()}}, otherwise, the results are undefined.
+
Reallocates the given area of memory ([[cpp/language/object#Object creation|implicitly creating]] objects in the destination area). It must be previously allocated by {{lc|std::malloc}}, {{lc|std::calloc}} or {{tt|std::realloc}} and not yet freed with {{lc|std::free}}, otherwise, the results are undefined.
  
 
The reallocation is done by either:
 
The reallocation is done by either:
@a@ expanding or contracting 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.  
+
@a@ expanding or contracting the existing area pointed to by {{c|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.
+
@b@ allocating a new memory block of size {{c|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.
  
If {{tt|ptr}} is {{lc|NULL}}, the behavior is the same as calling {{lc|std::malloc}}({{tt|new_size}}).
+
If {{c|ptr}} is a null pointer, the behavior is the same as calling {{c|std::malloc(new_size)}}.
  
If {{tt|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).
+
If {{c|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. {{rev inl|since=c++20|Such usage is deprecated (via {{stddoc|n2396.htm#dr_400|C DR 400|lang=c}}).}}
  
===Notes===
+
{{cpp/memory/thread_safety_note}}
Because reallocation may involve bytewise copying (regardless of whether it's to expand or to contract), only the objects of {{concept|TriviallyCopyable}} types are safe to access in the preserved part of the memory block after a call to {{tt|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
+
 
+
Objects of such type can be accessed after their storage is reallocated even if their copy constructors are not trivial.
+
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | ptr | pointer to the memory area to be reallocated}}
+
{{par|ptr|pointer to the memory area to be reallocated}}
{{par | new_size | new size of the array}}
+
{{par|new_size|new size of the array}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Pointer to the beginning of newly allocated memory or {{lc|NULL}} if error has occurred. The pointer must be deallocated with {{lc|std::free()}}.
+
On success, returns a pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with {{lc|std::free}} or {{tt|std::realloc}}. The original pointer {{c|ptr}} is invalidated and any access to it is [[cpp/language/ub|undefined behavior]] (even if reallocation was in-place).
 +
 
 +
On failure, returns a null pointer. The original pointer {{c|ptr}} remains valid and may need to be deallocated with {{lc|std::free}}.
 +
 
 +
===Notes===
 +
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 of {{named req|TriviallyCopyable}} type.
 +
 
 +
Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that does not 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===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
 +
#include <cassert>
 
#include <cstdlib>
 
#include <cstdlib>
#include <iostream>
 
#include <ostream>
 
 
#include <new>
 
#include <new>
#include <cassert>
 
  
 
class MallocDynamicBuffer
 
class MallocDynamicBuffer
 
{
 
{
     char *p;
+
     char* p;
 
+
 
public:
 
public:
     explicit MallocDynamicBuffer(std::size_t initial = 0);
+
     explicit MallocDynamicBuffer(std::size_t initial = 0) : p(nullptr)
    ~MallocDynamicBuffer();
+
 
+
    void resize(std::size_t newSize);
+
    char *ptr();
+
};
+
 
+
MallocDynamicBuffer::MallocDynamicBuffer(std::size_t initial)
+
    : p(NULL)
+
{
+
    if (!initial)
+
        return;
+
 
+
    void *mem = std::malloc(initial);
+
    if (!mem)
+
        throw std::bad_alloc();
+
    p = static_cast<char*>(mem);
+
    std::cout << "    Buffer now has size " << initial
+
              << " starting at address " << mem << '\n';
+
}
+
 
+
MallocDynamicBuffer::~MallocDynamicBuffer()
+
{
+
    std::free(p);
+
}
+
 
+
void MallocDynamicBuffer::resize(std::size_t newSize)
+
{
+
    if (!newSize)
+
 
     {
 
     {
         // realloc to size zero is implementation-defined.
+
         resize(initial);
        // We don't know portably what happens when it fails.
+
        // So, we instead use free().
+
        std::free(p);
+
        p = NULL;
+
 
     }
 
     }
     else
+
      
 +
    ~MallocDynamicBuffer() { std::free(p); }
 +
   
 +
    void resize(std::size_t newSize)
 
     {
 
     {
         void *mem = std::realloc(p, newSize);
+
         if (newSize == 0) // this check is not strictly needed,
         if (!mem)
+
        {
            throw std::bad_alloc();
+
            std::free(p); // but zero-size realloc is deprecated in C
   
+
            p = nullptr;
        p = static_cast<char*>(mem);
+
         }
 +
        else
 +
        {
 +
            if (void* mem = std::realloc(p, newSize))
 +
                p = static_cast<char*>(mem);
 +
            else
 +
                throw std::bad_alloc();
 +
        }
 
     }
 
     }
    std::cout << "    Buffer now has size " << newSize
 
              << " starting at address " << static_cast<void*>(p) << '\n';
 
}
 
  
char *MallocDynamicBuffer::ptr()
+
    char& operator[](size_t n) { return p[n]; }
{
+
     char operator[](size_t n) const { return p[n]; }
     return p;
+
};
}
+
  
int main()  
+
int main()
 
{
 
{
    std::cout << "buf1:\n";
 
 
     MallocDynamicBuffer buf1(1024);
 
     MallocDynamicBuffer buf1(1024);
     buf1.ptr()[5] = 'f';
+
     buf1[5] = 'f';
     buf1.resize(10);
+
     buf1.resize(10); // shrink
     assert(buf1.ptr()[5] == 'f');
+
     assert(buf1[5] == 'f');
   
+
     buf1.resize(1024); // grow
    std::cout << "buf2:\n";
+
     assert(buf1[5] == 'f');
    MallocDynamicBuffer buf2(512);
+
 
+
    std::cout << "buf1:\n";
+
     buf1.resize(1024);
+
     assert(buf1.ptr()[5] == 'f');
+
 
+
    return 0;
+
 
}
 
}
 
| output=buf1:
 
    Buffer now has size 1024 starting at address 0063D3B8
 
    Buffer now has size 10 starting at address 0063D3B8
 
buf2:
 
    Buffer now has size 512 starting at address 0063D3F0
 
buf1:
 
    Buffer now has size 1024 starting at address 0063DCD8
 
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see c | c/memory/realloc}}
+
{{dsc see c|c/memory/realloc}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/memory/c/realloc]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/memory/c/realloc]]
+
[[fr:cpp/memory/c/realloc]]
+
[[it:cpp/memory/c/realloc]]
+
[[ja:cpp/memory/c/realloc]]
+
[[pt:cpp/memory/c/realloc]]
+
[[ru:cpp/memory/c/realloc]]
+
[[zh:cpp/memory/c/realloc]]
+

Latest revision as of 17:21, 3 September 2023

 
 
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 (implicitly creating objects in the destination area). 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:

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 std::malloc(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:

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)

Contents

[edit] Parameters

ptr - pointer to the memory area to be reallocated
new_size - new size of the array

[edit] 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 std::free or std::realloc. 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.

[edit] Notes

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 of TriviallyCopyable type.

Some non-standard libraries define a type trait "BitwiseMovable" or "Relocatable", which describes a type that does not 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.

[edit] Example

#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);
            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');
}

[edit] See also

C documentation for realloc