Man page - realloc(3)

Packages contains this manual

Available languages:

en fr pl nl ja de

Manual

MALLOC

名 前
書 式
説 明
返 り 値
エ ラ ー
バ ー ジ ョ ン
属 性
準 拠
注 意
関 連 項 目
こ の 文 書 に つ い て

名 前

malloc, free, calloc, realloc, reallocarray - 動 的 な メ モ リ ー の 割 り 当 て と 解 放 を 行 う

書 式

#include <stdlib.h>

void *malloc(size_t size );
void free(void
*ptr );
void *calloc(size_t
nmemb , size_t size );
void *realloc(void
*ptr , size_t size );
void *reallocarray(void
*ptr , size_t nmemb , size_t size );

glibc 向 け の 機 能 検 査 マ ク ロ の 要 件 ( feature_test_macros (7) 参 照 ):

reallocarray ():
glibc 2.29 以 降 :
_DEFAULT_SOURCE
glibc 2.28 以 前 :
_GNU_SOURCE

説 明

malloc () 関 数 は size バ イ ト を 割 り 当 て 、 割 り 当 て ら れ た メ モ リ ー に 対 す る ポ イ ン タ ー を 返 す 。 メ モ リ ー の 内 容 は 初 期 化 さ れ な い size が 0 の 場 合 、 malloc () は NULL ま た は free () に 後 で 渡 し て も 問 題 の 起 こ ら な い 一 意 な ポ イ ン タ ー 値 を 返 す 。

free () 関 数 は ポ イ ン タ ー ptr が 指 す メ モ リ ー 空 間 を 解 放 す る 。 こ の ポ イ ン タ ー は 、 以 前 に 呼 び 出 さ れ た malloc (), calloc (), realloc () の い ず れ か が 返 し た 値 で な け れ ば な ら な い 。 こ れ 以 外 の ポ イ ン タ ー を 指 定 し た り 、 す で に free(ptr) が 実 行 さ れ て い た り し た 場 合 の 動 作 は 定 義 さ れ て い な い 。 ptr が NULL の 場 合 に は 、 何 の 動 作 も 行 わ れ な い 。

The calloc () function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc () returns either NULL, or a unique pointer value that can later be successfully passed to free (). If the multiplication of nmemb and size would result in integer overflow, then calloc () returns an error. By contrast, an integer overflow would not be detected in the following call to malloc (), with the result that an incorrectly sized block of memory would be allocated:

malloc(nmemb * size);

realloc () は 、 ポ イ ン タ ー ptr が 示 す メ モ リ ー ブ ロ ッ ク の サ イ ズ を size バ イ ト に 変 更 す る 。 領 域 の 先 頭 か ら 、 新 旧 の サ イ ズ の 小 さ い 方 の 位 置 ま で の 範 囲 の 内 容 は 変 更 さ れ な い 。 新 し い サ イ ズ が 前 の サ イ ズ よ り も 大 き い 場 合 、 追 加 さ れ た メ モ リ ー は 初 期 化 さ れ な い ptr が NULL の 場 合 に は malloc(size) と 等 価 で あ る 。 size が 0 で ptr が NULL で な い 場 合 に は 、 free(ptr) と 等 価 で あ る 。 ptr が NULL 以 外 の 場 合 、 ptr は 以 前 に 呼 び 出 さ れ た malloc (), calloc (), realloc () の い ず れ か が 返 し た 値 で な け れ ば な ら な い 。 ptr が 指 す 領 域 が 移 動 さ れ て い た 場 合 は free(ptr) が 実 行 さ れ る 。

The reallocarray () function changes the size of the memory block pointed to by ptr to be large enough for an array of nmemb elements, each of which is size bytes. It is equivalent to the call

realloc(ptr, nmemb * size);

However, unlike that realloc () call, reallocarray () fails safely in the case where the multiplication would overflow. If such an overflow occurs, reallocarray () returns NULL, sets errno to ENOMEM , and leaves the original block of memory unchanged.

返 り 値

関 数 calloc () と malloc () は 、 割 り 当 て ら れ た メ モ リ ー へ の ポ イ ン タ ー を 返 す 。 割 り 当 て ら れ た メ モ リ ー は 、 あ ら ゆ る 組 み 込 み 型 に 対 応 で き る よ う に ア ラ イ ン メ ン ト さ れ る 。 エ ラ ー の 場 合 、 こ れ ら の 関 数 は NULL を 返 す 。 size が 0 で 呼 び 出 し た malloc () や 、 nmemb size が 0 で 呼 び 出 し た calloc () が 成 功 し た 場 合 に も NULL が 返 さ れ る 。

free () 関 数 は 値 を 返 さ な い 。

The realloc () function returns a pointer to the newly allocated memory, which is suitably aligned for any built-in type, or NULL if the request failed. The returned pointer may be the same as ptr if the allocation was not moved (e.g., there was room to expand the allocation in-place), or different from ptr if the allocation was moved to a new address. If size was equal to 0, either NULL or a pointer suitable to be passed to free () is returned. If realloc () fails, the original block is left untouched; it is not freed or moved.

On success, the reallocarray () function returns a pointer to the newly allocated memory. On failure, it returns NULL and the original block of memory is left untouched.

エ ラ ー

calloc (), malloc (), realloc (), reallocarray () は 以 下 の エ ラ ー で 失 敗 す る こ と が あ る 。

ENOMEM

Out of memory. Possibly, the application hit the RLIMIT_AS or RLIMIT_DATA limit described in getrlimit (2).

バ ー ジ ョ ン

reallocarray () は glibc 2.26 で 初 め て 登 場 し た 。

属 性

こ の 節 で 使 用 さ れ て い る 用 語 の 説 明 に つ い て は 、 attributes (7) を 参 照 。

Image grohtml-45008-1.png

準 拠

malloc (), free (), calloc (), realloc (): POSIX.1-2001, POSIX.1-2008, C89, C99.

reallocarray () は 非 標 準 の 拡 張 で 、 OpenBSD 5.6 と FreeBSD 11.0 で 初 め て 登 場 し た 。

注 意

デ フ ォ ル ト で は 、 Linux は 楽 観 的 メ モ リ ー 配 置 戦 略 を 用 い て い る 。 つ ま り 、 malloc () が NULL で な い 値 を 返 し て も 、 そ の メ モ リ ー が 実 際 に 利 用 可 能 で あ る こ と が 保 証 さ れ な い 。 シ ス テ ム が メ モ リ ー 不 足 状 態 に な っ た と き 、 メ モ リ ー 不 足 解 決 器 (OOM killer) に よ っ て 一 つ ま た は 複 数 の プ ロ セ ス が 削 除 さ れ る 。 詳 し い 情 報 は 、 proc (5) の /proc/sys/vm/overcommit_memory proc/sys/vm/oom_adj 、 お よ び Linux カ ー ネ ル の ソ ー ス フ ァ イ ル の Documentation/vm/overcommit-accounting.rst を 参 照 の こ と 。

Normally, malloc () allocates memory from the heap, and adjusts the size of the heap as required, using sbrk (2). When allocating blocks of memory larger than MMAP_THRESHOLD bytes, the glibc malloc () implementation allocates the memory as a private anonymous mapping using mmap (2). MMAP_THRESHOLD is 128 kB by default, but is adjustable using mallopt (3). Prior to Linux 4.7 allocations performed using mmap (2) were unaffected by the RLIMIT_DATA resource limit; since Linux 4.7, this limit is also enforced for allocations performed using mmap (2).

マ ル チ ス レ ッ ド ア プ リ ケ ー シ ョ ン で の デ ー タ 破 損 を 回 避 す る た め 、 内 部 で は mutexを 使 用 し て 、 こ れ ら の 関 数 で 利 用 さ れ る メ モ リ ー 管 理 用 の デ ー タ 構 造 を 保 護 し て い る 。 複 数 の ス レ ッ ド が 同 時 に メ モ リ ー の 確 保 や 解 放 を 行 う よ う な マ ル チ ス レ ッ ド ア プ リ ケ ー シ ョ ン で は 、 こ れ ら の mutex の 衝 突 が 起 こ り 得 る 。 マ ル チ ス レ ッ ド ア プ リ ケ ー シ ョ ン で の メ モ リ ー 割 り 当 て 処 理 に ス ケ ー ラ ビ リ テ ィ を 持 た せ る た め に 、 glibc で は mutex の 衝 突 が 検 出 さ れ た 際 に は 追 加 の メ モ リ ー 割 り 当 て 領 域 を 作 成 す る 。 追 加 領 域 の 各 々 は 、 ( brk (2) や mmap (2) を 使 っ て ) シ ス テ ム に よ り 内 部 的 に 割 り 当 て ら れ た 大 き な 領 域 で 、 そ れ ぞ れ 独 自 の mutex に よ り 管 理 さ れ て い る 。

SUSv2 で は 、 malloc (), calloc (), realloc () は 実 行 に 失 敗 し た と き に errno ENOMEM に 設 定 す る こ と に な っ て い る 。 Glibc で は こ れ が 守 ら れ て い る こ と を 仮 定 し て い る (ま た こ れ ら の ル ー チ ン の glibc バ ー ジ ョ ン は こ の こ と を 守 っ て い る )。 個 人 的 に 別 の malloc の 実 装 を 使 っ て い て 、 そ の malloc が errno を 設 定 し な い 場 合 に は 、 失 敗 し た 際 に errno に エ ラ ー の 理 由 を 設 定 し な い ラ イ ブ ラ リ ル ー チ ン が あ る か も し れ な い 。

malloc (), calloc (), realloc (), free () に お け る 事 故 は 、 ほ と ん ど の 場 合 は ヒ ー プ の 破 壊 (corruption) が 原 因 で あ る 。 例 え ば 、 割 り 当 て ら れ た 領 域 を オ ー バ ー フ ロ ー す る 、 同 じ ポ イ ン タ ー に 二 度 free す る 、 な ど が こ れ に あ た る 。

malloc 実 装 は 、 環 境 変 数 で 動 作 を 調 整 で き る 。 詳 細 は mallopt (3) を 参 照 の こ と 。

関 連 項 目

valgrind (1), brk (2), mmap (2), alloca (3), malloc_get_state (3), malloc_info (3), malloc_trim (3), malloc_usable_size (3), mallopt (3), mcheck (3), mtrace (3), posix_memalign (3)

For details of the GNU C library implementation, see https://sourceware.org/glibc/wiki/MallocInternals .

こ の 文 書 に つ い て

こ の man ペ ー ジ は Linux man-pages プ ロ ジ ェ ク ト の リ リ ー ス 5.10 の 一 部 で あ る 。 プ ロ ジ ェ ク ト の 説 明 と バ グ 報 告 に 関 す る 情 報 は https://www.kernel.org/doc/man-pages/ に 書 か れ て い る 。