tags:

views:

156

answers:

5

Why would you ever want to use alloca() when you could always allocate a fixed size buffer on the stack large enough to fit all uses? This is not a rhetorical question...

+2  A: 

Never - it's not part of C++, and not useful in C. However, you cannot allocate "a static buffer on the stack" - static buffers are allocated at compile time, and not on the stack.

The point of alloca() is of course that it is not fixed sized, it is on the stack, and that it is freed automatically when a function exits. Both C++ and C have better mechanisms to handle this.

anon
static is never threadsafe, alloca is
Did zr edit the question? It now reads, "fixed-sized", not "static"
Shog9
alloca is no less a part of C++ than it is in C -- it is standardized in neither.
Heath Hunnicutt
@shog9 - if the question had been edited, we would see denotation of that. We do not.
Heath Hunnicutt
@Heath It has been edited - inside the initial limit, which does not show up in the edit history.
anon
@Heath: as Neil notes, there's a grace period following posting or editing wherein edits made by the author / last editor do not generate a separate revision entry. Given that Neil and zr both posted within a couple minutes of each other, I figured it was likely this had occurred...
Shog9
Ahh, who knew? Thanks.
Heath Hunnicutt
yes, i edited the question. By 'static' i meant 'fixed size known at compile time' and not 'static' like c/c++ keyword.
zr
A: 

You might want to use it if there's no way to know the maximum size you might need at compile time.

Whether you should is another question - it's not standard, and there's no way to tell whether it might cause a stack overflow.

Mike Seymour
*pat on the back* for "Stack Overflow"!
Ryan Tenney
A: 

In which cases is alloca() useful?

The only time I ever saw alloca being used was in Open Dynamics Engine. AFAIK they were allocating HUGE matrices with it (so compiled program could require 100MB stack), which were automatically freed when function returns (looks like smartpointer ripoff to me). This was quite a while ago.

Although it was probably much faster than new/malloc, I still think it was a bad idea. Instead of politely running out of RAM program could crash with stack overflow (i.e. misleading) when scene became too complex to handle. Not a nice behavior, IMO, especially for physics engine, where you can easily expect someone to throw few thousands bricks into scene and see what happens when they all collide at once. Plus you had to set stack size manually - i.e. on system with more RAM, program would be still limited by stack size.

a fixed size buffer on the stack large enough to fit all uses? This is not a rhetorical question...

If you need fixed-size buffer for all uses, then you could as well put it into static/global variable or use heap memory.

SigTerm
@SigTerm - As a "smartpointer" ripoff, I guess `alloca` used a time machine to steal the idea. `alloca` is from the late 1960's, and smart pointers are post-1986... The point has been made elsewhere about threadsafety as a positive of alloca that static/global variables do not share. The heap depends on how you use it, so not addressing that with 900 characters.
Heath Hunnicutt
@Heath Hunnicutt: "alloca used a time machine" Open Dynamics Engine wasn't written in 60s.
SigTerm
+2  A: 

It could be useful if the size of the buffer varies at runtime, or if you only sometimes need it: this would use less stack space overall than a fixed-size buffer in each call. Particularly if the function is high up the stack or recursive.

Andy Mortimer
+1 for the most convincing answer, so far. When a function is recursive, allocating the max possibly needed buffer size on the stack, could reduce significantly the number of max recursive calls. So using alloca() can reduce the penalty of needing to deal with the worst case scenario.
zr
A: 

The alloca() function is virtually never needed; for memory allocation purposes, you can use malloc()/free() in C (or one of the collection of possibilities in C++) and achieve pretty much the same practical effect. This has the advantage of coping better with smaller stack sizes.

However I have seen[1] one legit (if hacky!) use of it: for detecting potential stack overflow on Windows; if the allocation (of the amount of slop space you wanted to access) failed, you were out but had enough room to recover gracefully. It was wrapped in __try/__except so that it didn't crash, and needed extra assembler tricks to avoid gcc-induced trouble. As I said, a hack. But a clever one that is the only valid use for alloca() that I've ever seen.

But don't do that. Better to write the code to not need such games.


[1] It was in Tcl 8.4 (and possibly earlier versions of Tcl). It was removed in later versions. Later versions removed it because it was finicky, very tricky and deeply troubling. 8.6 uses a stackless implementation of the execution engine instead of that sort of funkiness.

Donal Fellows
FWIW: on Windows, there's usually a guard page at the end of the stack, used to expand it dynamically. Once the limit of the stack has been reached and this guard page hit, you get the exception - but just the one time. Unless they reset the guard page after detecting the end of the stack, this trick would only work once... Next time around, the program would be terminated immediately by the system.
Shog9
@Shog: Interesting, though something that we no longer need; we switched how our implementation engine works to no longer need a deep C stack. :-) I just thought it would be interesting to people as a use for alloca that cannot be duplicated at all by malloc.
Donal Fellows
@Donal: I agree - thanks for sharing!
Shog9