Sign in to follow this  
antareus

Largest size safe for alloca()?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this