c99数组长度规定

c99规定数组长度可以为变量,下面的代码合法:
int x ;
int array[x]

标准c++的规定不允许这样(没有具体查,多半我也看不懂c++的标准....)但gcc上面是允许的。
在这种情况下,gcc会调用alloca在栈上分配数据空间,alloca的manual如下:

NAME
       alloca - memory allocator

SYNOPSIS
       #include <alloca.h>

       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 func-
       tion that called alloca returns to its caller.

RETURN VALUE
       The alloca function returns a pointer to the beginning of the allocated
       space.  If the allocation causes stack overflow, program  behaviour  is
       undefined.

你可能感兴趣的:(C++,c,function,gcc,Allocation)