alloca()
allocates memory on the stack rather than on the heap, as in the case of malloc()
. So, when I return from the routine the memory is freed.
One of the most memorable bugs I had was to do with an inline function that used alloca
. It manifested itself as a stack overflow (because it allocates on the stack) at random points of the program's execution.
In the header file:
void DoSomething() {
wchar_t* pStr = alloca(100);
//......
}
In the implementation file:
void Process() {
for (i = 0; i < 1000000; i++) {
DoSomething();
}
}
So what happened was the compiler inlined DoSomething
function and all the stack allocations were happening inside Process()
function and thus blowing the stack up. In my defence (and I wasn't the one who found the issue; I had to go and cry to one of the senior developers when I couldn't fix it), it wasn't straight alloca
, it was one of ATL string conversion macros.
So the lesson is - do not use alloca
in functions that you think might be inlined.
I don't think that anybody has mentioned this, but alloca also has some serious security issues not necessarily present with malloc (though these issues also arise with any stack based arrays, dynamic or not). Since the memory is allocated on the stack, buffer overflows/underflows have much more serious consequences than with just malloc.
In particular, the return address for a function is stored on the stack. If this value gets corrupted, your code could be made to go to any executable region of memory. Compilers go to great lengths to make this difficult (in particular by randomizing address layout). However, this is clearly worse than just a stack overflow since the best case is a SEGFAULT if the return value is corrupted, but it could also start executing a random piece of memory or in the worst case some region of memory which compromises your program's security.
All of the other answers are correct. However, if the thing you want to alloc using alloca()
is reasonably small, I think that it's a good technique that's faster and more convenient than using malloc()
or otherwise.
In other words, alloca( 0x00ffffff )
is dangerous and likely to cause overflow, exactly as much as char hugeArray[ 0x00ffffff ];
is. Be cautious and reasonable and you'll be fine.
Processes only have a limited amount of stack space available - far less than the amount of memory available to malloc()
.
By using alloca()
you dramatically increase your chances of getting a Stack Overflow error (if you're lucky, or an inexplicable crash if you're not).
Most answers here largely miss the point: there's a reason why using _alloca()
is potentially worse than merely storing large objects in the stack.
The main difference between automatic storage and _alloca()
is that the latter suffers from an additional (serious) problem: the allocated block is not controlled by the compiler, so there's no way for the compiler to optimize or recycle it.
Compare:
while (condition) {
char buffer[0x100]; // Chill.
/* ... */
}
with:
while (condition) {
char* buffer = _alloca(0x100); // Bad!
/* ... */
}
The problem with the latter should be obvious.
As noted in this newsgroup posting, there are a few reasons why using alloca
can be considered difficult and dangerous:
alloca
.alloca
differently, so portability is not guaranteed even between compilers that support it.