alloca可以完全替代吗?
我读过很多地方说 alloca
已经过时,不应该使用,而应该使用可变长度数组。
我的问题是:alloca 是否完全可以被可变长度数组替代?
在我的特定实例中,我有一些看起来像这样的东西:
typedef struct {
int *value;
size_t size;
} some_type;
void SomeExternalFunction(some_type);
...
void foo(){
//What I thought to do
some_type bar;
bar.value=alloca(sizeof(int)*10);
SomeExternalFunction(bar);
//what should be done without alloca
some_type fizz;
int tmp[10];
fizz.value=tmp;
SoemExternalFunction(fizz);
}
我是否遗漏了一些东西,或者这是alloca的实际良好用途?还假设在此示例中,出于某种原因,我希望在堆栈上分配该值
I've read quite a few places that alloca
is obsolete and should not be used and Variable Length Arrays should be used instead.
My question is this: Is alloca
completely replaceable by variable length arrays?
In my particular instance I have something that looks like this:
typedef struct {
int *value;
size_t size;
} some_type;
void SomeExternalFunction(some_type);
...
void foo(){
//What I thought to do
some_type bar;
bar.value=alloca(sizeof(int)*10);
SomeExternalFunction(bar);
//what should be done without alloca
some_type fizz;
int tmp[10];
fizz.value=tmp;
SoemExternalFunction(fizz);
}
Am I missing something or is this an actual good use of alloca? Also assume for this example that for some reason I want for the value to be allocated on the stack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
VLA 和 alloca 之间有一个重要区别:只要当前函数持续存在,alloca() 返回的内存就有效。只要 VLA 的标识符保留在范围内,VLA 占用的内存的生命周期就有效。例如,您可以在循环中分配内存并在循环外部使用内存,VLA 将消失,因为当循环终止时标识符超出范围。这意味着,您可以使用 alloca() 和足够的堆栈空间来执行此操作:
但不能使用 VLA 执行此操作。
There is an important difference between VLA's and alloca: The memory alloca() returns is valid as long as the current function persists. The lifetime of the memory occupied by a VLA is valid as long as the VLA's identifier remains in scope. You can alloca() memory in a loop for example and use the memory outside the loop, a VLA would be gone because the identifier goes out of scope when the loop terminates. This means, you can do this with alloca() and enough stack space:
You can't do this with VLAs.
alloca
完全可以被malloc
和free
取代。这需要更多的工作,但除非您非常小心,否则这是必不可少的。几乎所有使用alloca
或 C99 vla 的代码都容易受到堆栈溢出攻击,并且在许多实现中,它们可能会导致特权提升。没有可移植的方法来知道堆栈有多大或剩余多少堆栈空间(或者编译器的内部使用或进一步的函数调用可能需要超出请求大小的多少开销),因此您可以做的唯一合理的事情是使 vla's/alloca
安全是对您支持的数据大小(例如几 kb)施加极小的人为限制。此时,您可能只使用普通的非可变长度自动对象......alloca
is completely replaceable bymalloc
andfree
. It's a little more work but unless you're very careful, it's essential. Nearly all code usingalloca
or C99 vla's is vulnerable to stack overflow attacks, and in many implementations, they can lead to privilege elevation. There's no portable way to know how large the stack is or how much stack space is left (or how much overhead beyond the requested size might be needed for the compiler's internal use or further function calls), so the only reasonable thing you can do to make vla's/alloca
safe is impose extremely small artificial limits on the size of data you support (e.g. a few kb). At this point you might as well just be using plain non-variable-length automatic objects...