Skip to content

Commit 8233b0a

Browse files
committed
new page cstdlib/free
1 parent 443e16e commit 8233b0a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

reference/cstdlib/free.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# free
2+
* cstdlib[meta header]
3+
* std[meta namespace]
4+
* function[meta id-type]
5+
6+
```cpp
7+
namespace std {
8+
void free( void *ptr );
9+
}
10+
```
11+
12+
## 概要
13+
`malloc()`、`calloc()`、`aligned_alloc`、`realloc`で確保されたメモリ領域を解放する。
14+
15+
もし上記の4つの関数で渡されたポインタ以外をこの関数の引数にした場合は、未定義動作となる。
16+
17+
また、渡されたメモリがすでに解放されている場合も、未定義動作となる。
18+
19+
ポインタが`nullptr`の場合、この関数は何も実行しない。
20+
21+
解放されたポインタに再びアクセスする場合、未定義動作を引き起こす恐れがある。
22+
23+
## 備考
24+
この関数はスレッドセーフである。
25+
26+
## 実装例
27+
```cpp example
28+
int main(void)
29+
{
30+
int *p1 = malloc(10*sizeof *p1);
31+
free(p1); // p1のメモリを解放
32+
33+
int *p2 = calloc(10, sizeof *p2);
34+
int *p3 = realloc(p2, 1000*sizeof *p3);
35+
if(p3)
36+
free(p3);//p2は`null`ではないので、p3が解放される。
37+
else
38+
free(p2);
39+
}
40+
```
41+
### 出力結果
42+
```
43+
```
44+
45+
## 関連項目
46+
- [`aligned_alloc`](aligned_alloc.md):指定したアライメントでメモリを確保する
47+
- [`malloc`](malloc.md):メモリを確保する
48+
- [`calloc`](calloc.md):メモリを確保する(0で初期化する)
49+
- [`realloc`](realloc.md.nolink):メモリを再確保する

0 commit comments

Comments
 (0)