此代码是否返回对分配在堆栈上的变量的无效引用?或者什么:
void *f(size_t sz) {
return alloca(sz);
}
或者它是由 alloca 实现/编译器支持处理的特殊情况,如 f(alloca(size), alloca(size))
会是什么?
最佳答案
alloca
在f
的堆栈帧中分配空间。一旦函数返回,您就不能用它做任何有用的事情(它不再是“保留的”)。
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.
关于c - 返回分配指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7048980/