Jar*_*tar 7 c objective-c objective-c++ alloca
有什么区别
void *bytes = alloca(size);
Run Code Online (Sandbox Code Playgroud)
和
char bytes[size]; //Or to be more precise, char x[size]; void *bytes = x;
Run Code Online (Sandbox Code Playgroud)
...其中size是一个在编译时值未知的变量.
Bil*_*eal 15
alloca() 在当前函数结束之前不会回收内存,而可变长度数组在当前块结束时回收内存.
换一种方式:
void foo()
{
size_t size = 42;
if (size) {
void *bytes1 = alloca(size);
char bytes2[size];
} // bytes2 is deallocated here
}; //bytes1 is deallocated here
Run Code Online (Sandbox Code Playgroud)
alloca() 可以在任何C89编译器上支持(以某种方式),而可变长度数组需要C99编译器.