Largest size safe for alloca()?

Started by
1 comment, last by iMalc 18 years, 9 months ago
alloca() is a wonderful function for those of us who don't have C99 compilers, as it dynamically allocates the specified number of bytes from the stack. The problem is, if you ask for too much, you may trigger a stack overflow. Obviously, this isn't acceptable. Under Win32, you can trap the SEH and move on, but I'm not sure what a reasonable upper limit on the number of bytes one should stack allocate would be. Perhaps somewhere in the area of 4096 bytes? Anything beyond that would need more than one page of memory, so it seems like a decent starting point. I haven't played with the default stack size in MSVC7.1, so I think its still at 1 megabyte.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Advertisement
the default stack size is still 1 meg. You can alloca up to that amount, minus what ever is on the stack at the moment. You can alloca more than 4kb safely, I've seen up to 128K in our apps using the ATL A2T macro's on large xml strings.

This also seems to be ok for Linux and OS X.

Cheers
Chris
CheersChris
Basically, if you have any real doubts about whether there will be enough stack space or not, then you should allocate from the heap instead, IMHO. Even if it's a close call, later changes can make it no longer fit, and I don't think one should really have to adjust the default stack size, except where exceptional circumstances make it a necessity.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement