Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::malloc

      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* malloc(std::size_t size);

      Allocatessize bytes of uninitialized storage.

      If allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any scalar type (at least as strictly asstd::max_align_t) (implicitly creating objects in the destination area).

      Ifsize 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, but has to be passed tostd::free).

      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

      size - number of bytes to allocate

      [edit]Return value

      On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated withstd::free() orstd::realloc().

      On failure, returns a null pointer.

      [edit]Notes

      This function does not call constructors or initialize memory in any way. There are no ready-to-use smart pointers that could guarantee that the matching deallocation function is called. The preferred method of memory allocation in C++ is using RAII-ready functionsstd::make_unique,std::make_shared, container constructors, etc, and, in low-level library code,new-expression.

      For loading a large file, file mapping via OS-specific functions, e.g.mmap on POSIX orCreateFileMapping(A/W) along withMapViewOfFile on Windows, is preferable to allocating a buffer for file reading.

      [edit]Example

      Run this code
      #include <cstdlib>#include <iostream>#include <memory>#include <string> int main(){constexprstd::size_t size=4;if(auto ptr=reinterpret_cast<std::string*>(std::malloc(size* sizeof(std::string)))){try{for(std::size_t i=0; i< size;++i)std::construct_at(ptr+ i,5,'a'+ i);for(std::size_t i=0; i< size;++i)std::cout<<"ptr["<< i<<"] == "<< ptr[i]<<'\n';std::destroy_n(ptr, size);}catch(...){}std::free(ptr);}}

      Output:

      p[0] == aaaaap[1] == bbbbbp[2] == cccccp[3] == ddddd

      [edit]See also

      allocation functions
      (function)[edit]
      (deprecated in C++17)(removed in C++20)
      obtains uninitialized storage
      (function template)[edit]
      C documentation formalloc
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/c/malloc&oldid=179180"

      [8]ページ先頭

      ©2009-2025 Movatter.jp