std::malloc(3)
π NAME π
Bestie, welcome to std::malloc(3) from the C++ Standard Libary β and the NAME line is literally std::malloc - std::malloc. Thatβs it. The function is naming itself like main character energy ππ
.
πΈ Synopsis πΈ
Defined in header <cstdlib>
void* malloc( std::size_t size );
Okay so this one allocates size bytes of uninitialized storage. If allocation succeeds, it 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 as std::max_align_t) (implicitly creating objects in the destination area) β¨.
Ifsizeis zero, the behavior is implementation defined: a 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:
- The library versions of
operator newandoperator delete - User replacement versions of global
operator newandoperator delete std::calloc,std::malloc,std::realloc(since C++11),std::aligned_alloc(since C++17),std::free
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 π«Ά.
π Parameters π
sizeβ number of bytes to allocate.
π¦ 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 with std::free() or std::realloc() π.
On failure, returns a null pointer π.
β¨ 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 functions std::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 or CreateFileMapping(A/W) along with MapViewOfFile on Windows, is preferable to allocating a buffer for file reading π.
π₯ Example π₯
Run this code, bestie:
// Run this code
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
int main()
{
constexpr std::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] == aaaaa
p[1] == bbbbb
p[2] == ccccc
p[3] == ddddd
π See also π
operator newβ allocation functionsoperator new[]β (function)get_temporary_bufferβ obtains uninitialized storage (deprecated in C++17) (function template) (removed in C++20)- C documentation for
malloc
Thatβs the footer, bestie: source/version http://cppreference.com, date 2024.06.10, manual std::malloc(3). Mwah ππ«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms