File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ # calloc
2+ * cstdlib[ meta header]
3+ * std[ meta namespace]
4+ * function[ meta id-type]
5+
6+ ## 概要
7+
8+ ```
9+ void *calloc(size_t nmemb, size_t size);
10+ ```
11+
12+ 任意の個数のオブジェクトへの配列用ポインタを動的に確保し、その領域を0で初期化する。
13+
14+ この関数で返されるポインタは、割当領域の先頭のポインタである。
15+
16+ 領域が割り当てられない場合は、[ ` nullptr ` ] ( /lang/cpp11/nullptr.md ) を返す。
17+
18+ 割り当てられたオブジェクトの寿命は、このメモリが割り当てられてられた時から、[ ` free ` ] ( free.md.nolink ) などで解放されるまでである。
19+
20+ この関数は、静的ストレージにはアクセスしない、スレッドセーフである。
21+
22+ ## 備考
23+
24+ - ` calloc ` 、` malloc ` および ` realloc ` 関数による連続的なメモリ確保によるメモリの割当の順序、連続性は、未定義である。
25+
26+ ## 例
27+
28+ ``` cpp example
29+ #include < stdio.h>
30+ #include < stdlib.h>
31+
32+ int main (void)
33+ {
34+ int* p1 = calloc(4, sizeof(int)); // 4個のint配列のポインタを確保、0二初期化
35+ int* p2 = calloc(1, sizeof(int[ 4] )); // 上と同じ
36+ int* p3 = calloc(4, sizeof * p3); // 上と同じ
37+
38+ if (p2)
39+ {
40+ for (int n = 0; n < 4; ++n)
41+ printf("p2[%d] == %d\n", n, p2[n]);
42+ }
43+
44+ free(p1);
45+ free(p2);
46+ free(p3);
47+ }
48+ ```
49+
50+ ## 出力例
51+ ```
52+ p2[ 0] == 0
53+ p2[ 1] == 0
54+ p2[ 2] == 0
55+ p2[ 3] == 0
56+ ```
57+
58+ ## 関連項目
59+ - ["free"]("free.md.nolink"):確保されたメモリを解放する。
60+
61+
62+
63+ ## 参照
64+ -[7.20.3 Memory management functions]("https://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf")
65+
You can’t perform that action at this time.
0 commit comments