Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::realloc

      From cppreference.com
      <cpp‎ |memory‎ |c
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(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)
      (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 bystd::malloc,std::calloc orstd::realloc and not yet freed withstd::free, otherwise, the results are undefined.

      The reallocation is done by either:

      a) expanding or contracting the existing area pointed to byptr, 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 sizenew_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.

      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

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

      [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 ofTriviallyCopyable 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

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

      [edit]See also

      C documentation forrealloc
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/c/realloc&oldid=179182"

      [8]ページ先頭

      ©2009-2025 Movatter.jp