diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/memory%2Fmalloc.html | |
new repository
Diffstat (limited to 'devdocs/c/memory%2Fmalloc.html')
| -rw-r--r-- | devdocs/c/memory%2Fmalloc.html | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/devdocs/c/memory%2Fmalloc.html b/devdocs/c/memory%2Fmalloc.html new file mode 100644 index 00000000..05f8d1f2 --- /dev/null +++ b/devdocs/c/memory%2Fmalloc.html @@ -0,0 +1,52 @@ + <h1 id="firstHeading" class="firstHeading">malloc</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">void *malloc( size_t size );</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Allocates <code>size</code> bytes of uninitialized storage.</p> +<p>If allocation succeeds, returns a pointer that is suitably aligned for any object type with <a href="../language/object#Alignment" title="c/language/object">fundamental alignment</a>.</p> +<p>If <code>size</code> is zero, the behavior of <code>malloc</code> is implementation-defined. For example, a null pointer may be returned. Alternatively, a non-null pointer may be returned; but such a pointer should not be <a href="../language/operator_member_access" title="c/language/operator member access">dereferenced</a>, and should be passed to <code><a href="free" title="c/memory/free">free</a></code> to avoid memory leaks.</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c11"> +<td> <p><code>malloc</code> is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.</p> +<p>A previous call to <code><a href="free" title="c/memory/free">free</a></code> or <code><a href="realloc" title="c/memory/realloc">realloc</a></code> that deallocates a region of memory <i>synchronizes-with</i> a call to <code>malloc</code> that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory by <code>malloc</code>. There is a single total order of all allocation and deallocation functions operating on each particular region of memory.</p> +</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td> +</tr> </table> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> size </td> <td> - </td> <td> number of bytes to allocate </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with <code><a href="free" title="c/memory/free">free()</a></code> or <code><a href="realloc" title="c/memory/realloc">realloc()</a></code>.</p> +<p>On failure, returns a null pointer.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <stdlib.h> + +int main(void) +{ + int *p1 = malloc(4*sizeof(int)); // allocates enough for an array of 4 int + int *p2 = malloc(sizeof(int[4])); // same, naming the type directly + int *p3 = malloc(4*sizeof *p3); // same, without repeating the type name + + if(p1) { + for(int n=0; n<4; ++n) // populate the array + p1[n] = n*n; + for(int n=0; n<4; ++n) // print it back out + printf("p1[%d] == %d\n", n, p1[n]); + } + + free(p1); + free(p2); + free(p3); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">p1[0] == 0 +p1[1] == 1 +p1[2] == 4 +p1[3] == 9</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.22.3.4 The malloc function (p: 254) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.22.3.4 The malloc function (p: 349) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.20.3.3 The malloc function (p: 314) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.10.3.3 The malloc function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="free" title="c/memory/free"> <span class="t-lines"><span>free</span></span></a></div> </td> <td> deallocates previously allocated memory <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/memory/c/malloc" title="cpp/memory/c/malloc">C++ documentation</a></span> for <code>malloc</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/memory/malloc" class="_attribution-link">https://en.cppreference.com/w/c/memory/malloc</a> + </p> +</div> |
