c - 返回分配指针

标签 c pointers memory-management stack alloca

此代码是否返回对分配在堆栈上的变量的无效引用?或者什么:

void *f(size_t sz) {
    return alloca(sz);
}

或者它是由 alloca 实现/编译器支持处理的特殊情况,如 f(alloca(size), alloca(size)) 会是什么?

最佳答案

allocaf 的堆栈帧中分配空间。一旦函数返回,您就不能用它做任何有用的事情(它不再是“保留的”)。

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/

相关文章:

iphone - 是否有必要在 iphone 中发布 NSInteger?

C++ : about memory management

c - 我的随 secret 码生成器抛出错误。我该如何解决?

c - 如何从队列中删除集合节点

c - 休息;未选择正确选项时出现问题。

c - 使用 CUDA-C 生成索引

c++ - 有没有更好的方法来初始化 C++ 中分配的数组?

c++ - C++ 中使用 char* 的指针算术

c - 确定存储指向其他不同维度数组的指针的数组的类型

java - 如何让这个java程序在多核上运行?