Difference between revisions of "cpp/memory/c/malloc"
m (→Example: avoid basic_string) |
(→Notes: + file mapping) |
||
Line 25: | Line 25: | ||
===Notes=== | ===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 {{lc|std::make_unique}}, {{lc|std::make_shared}}, container constructors, etc, and, in low-level library code, [[cpp/language/new|new-expression]]. | 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 {{lc|std::make_unique}}, {{lc|std::make_shared}}, container constructors, etc, and, in low-level library code, [[cpp/language/new|new-expression]]. | ||
+ | |||
+ | For loading a large file, it is preferred to use file mapping with OS-specific functions, e.g. [https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html {{tt|mmap}}] on POSIX or {{tt|CreateFileMapping}}([https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga {{tt|A}}]/[https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-createfilemappingw {{tt|W}}]) along with [https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile {{tt|MapViewOfFile}}] on Windows. | ||
===Example=== | ===Example=== |
Revision as of 17:28, 11 February 2021
Defined in header <cstdlib>
|
||
void* malloc( std::size_t size ); |
||
Allocates size
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.
If 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, but has to be passed to std::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 call happens-before the next allocation (if any) in this order. |
(since C++11) |
Contents |
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, it is preferred to use file mapping with OS-specific functions, e.g. mmap
on POSIX or CreateFileMapping
(A
/W
) along with MapViewOfFile
on Windows.
Example
#include <iostream> #include <cstdlib> #include <string> int main() { // allocates enough for an array of 4 strings if(auto p = (std::string*)std::malloc(4 * sizeof(std::string))) { int i = 0; try { for(; i != 4; ++i) // populate the array new(p + i) std::string(5, 'a' + i); for(int j = 0; j != 4; ++j) // print it back out std::cout << "p[" << j << "] == " << p[j] << '\n'; } catch(...) {} using std::string; for(; i != 0; --i) // clean up p[i - 1].~string(); std::free(p); } }
Output:
p[0] == aaaaa p[1] == bbbbb p[2] == ccccc p[3] == ddddd
See also
allocation functions (function) | |
(deprecated in C++17)(removed in C++20) |
obtains uninitialized storage (function template) |
C documentation for malloc
|