diff options
Diffstat (limited to 'devdocs/c/memory%2Fcalloc.html')
| -rw-r--r-- | devdocs/c/memory%2Fcalloc.html | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/devdocs/c/memory%2Fcalloc.html b/devdocs/c/memory%2Fcalloc.html new file mode 100644 index 00000000..4c0abf87 --- /dev/null +++ b/devdocs/c/memory%2Fcalloc.html @@ -0,0 +1,62 @@ + <h1 id="firstHeading" class="firstHeading">calloc</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* calloc( size_t num, size_t size );</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Allocates memory for an array of <code>num</code> objects of <code>size</code> and initializes all bytes in the allocated storage to zero.</p> +<p>If allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block 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 is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage).</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c11"> +<td> <p><code>calloc</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>calloc</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>calloc</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> num </td> <td> - </td> <td> number of objects </td> +</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> size of each object </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="Notes"> Notes</h3> <p>Due to the alignment requirements, the number of allocated bytes is not necessarily equal to <code>num * size</code>.</p> +<p>Initialization to all bits zero does not guarantee that a floating-point or a pointer would be initialized to <span class="nu16">0.0</span> and the null pointer value, respectively (although that is true on all common platforms).</p> +<p>Originally (in C89), support for zero size was added to accommodate code such as</p> +<div class="c source-c"><pre data-language="c">OBJ* p = calloc(0, sizeof(OBJ)); // "zero-length" placeholder +... +while(1) +{ + p = realloc(p, c * sizeof(OBJ)); // reallocations until size settles + ... // code that may change c or break out of loop +}</pre></div> <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 = calloc(4, sizeof(int)); // allocate and zero out an array of 4 int + int* p2 = calloc(1, sizeof(int[4])); // same, naming the array type directly + int* p3 = calloc(4, sizeof *p3); // same, without repeating the type name + + if (p2) + { + for (int n = 0; n < 4; ++n) // print the array + printf("p2[%d] == %d\n", n, p2[n]); + } + + free(p1); + free(p2); + free(p3); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">p2[0] == 0 +p2[1] == 0 +p2[2] == 0 +p2[3] == 0</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 7.22.3.2 The calloc function (p: TBD) </li></ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.22.3.2 The calloc function (p: 253) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.22.3.2 The calloc function (p: 348) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.20.3.1 The calloc function (p: 313) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.10.3.1 The calloc function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/memory/c/calloc" title="cpp/memory/c/calloc">C++ documentation</a></span> for <code>calloc</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/calloc" class="_attribution-link">https://en.cppreference.com/w/c/memory/calloc</a> + </p> +</div> |
