allocaのヘルプ・マニュアル
日本語 英語
alloca --help
man alloca
ALLOCA(3) Linux Programmer’s Manual ALLOCA(3)
名前
alloca - 自動的に解放されるメモリを割り当てる
書式
#include
void *alloca(size_t size);
説明
alloca() 関数は、 size バイトの領域を呼出元のスタック・フレームに割り付
ける。この一時的な領域は、 alloca() を呼び出した関数が呼出元に返ると き
に自動的に解放される。
返り値
alloca() 関数は、割り付けた領域の始まりを指すポインタを返す。割り付けに
よってスタックオーバーフローが起った場合のプログラムの動作は定義され て
いない。
準拠
この関数は POSIX.1-2001 にはない。
32V, PWB, PWB.2, 3BSD, 4BSD に alloca() 関数が登場した証拠がある。
4.3BSD には、マニュアルページがある。 Linux は、GNU 版を使っている。 こ
の関数は POSIX.1-2001 にはない。
注意
alloca() 関数は、機種とコンパイラに依存する。特定のアプリケーションでは
、この関数を使うと malloc(3) と free(3) を組み合わせて使った場合に比 べ
て 効 率 を改善することができる。特定の場合では、この関数を使うことで、
longjmp(3) や siglongjmp(3) を使うアプリケーションでのメモリの開放を 簡
単 にすることができる。それ以外の場合では、この関数の使用は推奨されない
。
alloca() により割り当てられる空間はスタックフレームから割り当てらるので
、関数の戻り先が longjmp(3) や siglongjmp(3) の呼び出しによりジャンプし
た場合には、割り当てられた空間は自動的に解放される。
alloca() で割り当てられた空間を free(3) しようとすることのないように!
注意
GNU 版についての注意
通常 gcc(1) は alloca() の呼び出しをインラインコードに変換する。 -ansi,
-std=c89, -std=c99, -fno-builtin のいずれかのオプションが指定された場合
、この変換は行われない (また のインクルードも行われない)。だ
だし、デフォルトでは glibc 版の は をインクルード
しており、これには以下の行が含まれているので注意すること。
#define alloca(size) __builtin_alloca (size)
独自版の __builtin_alloca (size) 関数があると厄介な結果になる。
このコードはインライン化されているので、この関数のアドレスを取得した り
、他のライブラリをリンクして動作を変更することはできない。
通 常 こ の イ ン ラ イ ンコードはスタックポインタを移動する 1 つの命令
(instruction) から構成されており、スタックオーバーフローをチェックし な
い。よって NULL エラーが返されることはない。
バグ
ス タックフレームが拡張できなかった場合、エラー通知は行われない。 (しか
しながら、割り当てに失敗した後で、プログラムが割り当てられなかった空 間
にアクセスしようとした場合に SIGSEGV シグナルを受信することだろう。)
多 くのシステムにおいて、関数コールの引き数のリスト内では alloca() が使
えない。これは、 alloca() によって予約されるスタック領域が、関数引き 数
に使われるスタック領域の中に現れてしまうためである。
関連項目
brk(2), longjmp(3), malloc(3)
GNU 2008-01-24 ALLOCA(3)
ALLOCA(3) Linux Programmer’s Manual ALLOCA(3)
NAME
alloca - allocate memory that is automatically freed
SYNOPSIS
#include
void *alloca(size_t size);
DESCRIPTION
The alloca() function allocates size bytes of space in the stack frame
of the caller. This temporary space is automatically freed when the
function that called alloca() returns to its caller.
RETURN VALUE
The alloca() function returns a pointer to the beginning of the allo-
cated space. If the allocation causes stack overflow, program behavior
is undefined.
CONFORMING TO
This function is not in POSIX.1-2001.
There is evidence that the alloca() function appeared in 32V, PWB,
PWB.2, 3BSD, and 4BSD. There is a man page for it in 4.3BSD. Linux
uses the GNU version.
NOTES
The alloca() function is machine- and compiler-dependent. For certain
applications, its use can improve efficiency compared to the use of
malloc(3) plus free(3). In certain cases, it can also simplify memory
deallocation in applications that use longjmp(3) or siglongjmp(3).
Otherwise, its use is discouraged.
Because the space allocated by alloca() is allocated within the stack
frame, that space is automatically freed if the function return is
jumped over by a call to longjmp(3) or siglongjmp(3).
Do not attempt to free(3) space allocated by alloca()!
Notes on the GNU Version
Normally, gcc(1) translates calls to alloca() with inlined code. This
is not done when either the -ansi, -std=c89, -std=c99, or the
-fno-builtin option is given (and the header is not
included). But beware! By default the glibc version of
includes and that contains the line:
#define alloca(size) __builtin_alloca (size)
with messy consequences if one has a private version of this function.
The fact that the code is inlined means that it is impossible to take
the address of this function, or to change its behavior by linking with
a different library.
The inlined code often consists of a single instruction adjusting the
stack pointer, and does not check for stack overflow. Thus, there is
no NULL error return.
BUGS
There is no error indication if the stack frame cannot be extended.
(However, after a failed allocation, the program is likely to receive a
SIGSEGV signal if it attempts to access the unallocated space.)
On many systems alloca() cannot be used inside the list of arguments of
a function call, because the stack space reserved by alloca() would
appear on the stack in the middle of the space for the function argu-
ments.
SEE ALSO
brk(2), longjmp(3), malloc(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2008-01-24 ALLOCA(3)