[c.malloc]

# 20 Memory management library [[mem]](./#mem)

## 20.2 Memory [[memory]](memory#c.malloc)

### 20.2.12 C library memory allocation [c.malloc]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2131)

[*Note [1](#note-1)*:

The header [<cstdlib>](cstdlib.syn#header:%3ccstdlib%3e "17.2.2 Header <cstdlib> synopsis [cstdlib.syn]") declares the functions described in this subclause[.](#1.sentence-1)

â *end note*]

[ð](#lib:aligned_alloc)

`void* aligned_alloc(size_t alignment, size_t size);
void* calloc(size_t nmemb, size_t size);
void* malloc(size_t size);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2147)

*Effects*: These functions have the semantics specified in the C standard library[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2151)

*Remarks*: These functions do not attempt to allocate
storage by calling ​::​operator new() ([[new.delete]](new.delete "17.6.3 Storage allocation and deallocation"))[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2157)

These functions implicitly create objects ([[intro.object]](intro.object "6.8.2 Object model"))
in the returned region of storage and
return a pointer to a suitable created object[.](#4.sentence-1)

In the case of calloc,
the objects are created before the storage is zeroed[.](#4.sentence-2)

[ð](#lib:realloc)

`void* realloc(void* ptr, size_t size);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2171)

*Preconditions*: free(ptr) has well-defined behavior[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2175)

*Effects*: If ptr is not null and size is zero,
the behavior is erroneous and the effects are implementation-defined[.](#6.sentence-1)

Otherwise, this function has the semantics specified in the C standard library[.](#6.sentence-2)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2181)

*Remarks*: This function does not attempt to allocate storage
by calling ​::​operator new() ([[new.delete]](new.delete "17.6.3 Storage allocation and deallocation"))[.](#7.sentence-1)

When a non-null pointer is returned,
this function implicitly creates objects ([[intro.object]](intro.object "6.8.2 Object model"))
in the returned region of storage and
returns a pointer to a suitable created object[.](#7.sentence-2)

The objects are created before the storage is copied[.](#7.sentence-3)

[ð](#lib:free)

`void free(void* ptr);
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2198)

*Effects*: This function has the semantics specified in the C standard library[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L2202)

*Remarks*: This function does not attempt to
deallocate storage by calling​::​operator delete()[.](#9.sentence-1)

See also: ISO/IEC 9899:2024, 7.22.3
