Scroll to navigation

MALLOC(3) Linux Programmer's Manual MALLOC(3)

名前

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() や、nmembsize が 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() は以下のエラーで失敗することがある。

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

バージョン

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

属性

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

インターフェース 属性
malloc(), free(), calloc(), realloc() Thread safety MT-Safe

準拠

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_memoryproc/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() は実行に失敗したときに errnoENOMEM に設定することになっている。 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/ に書かれている。

2020-06-09 GNU