Sign in to follow this  
Hermes

Stack size and page guard(need HELP)

Recommended Posts

Hi ya people i need to find out what is all about the stack and page guard.When i querry for the page dimension like GetSystemInfo i get a page dimension of 4MB .The compiler(VisualC++6.0)uses a default stack size of 1MB (is this for each calling thread cause if it is than every recursion i have has 1MB for each call).Is the dimension of the stack equal to needed size(objects put on the stack per thread basis)+PageGuardSize ?[looksaround] cause if it is then it's a little bit funny objectSize+4=1 I know a little bit of algebre[lol] and i know this is not right. A page guard should prevent going beyond the stack limit.So maybe a have 5MB of stack and i'm not aware of it.Is this true? Could i overcome the access violation errors(folowing the stack overflow)by just allocatin a new page,so i extend my stack on the run as i need it.Or it would be better to redimension the stack size at link time. Thanks for your support!

Share this post


Link to post
Share on other sites
If you stack overflow you are SOL. You can't rely on recovering from that...MSDN speaks of stack overflow or stack corruption as generally unrecoverable.

Fix the problem -- the stack overflow -- not the symptom.

Share this post


Link to post
Share on other sites
You should *never* really need a stack bigger than 1MB. The stack overflow signifies that either:
You have an infinite recursion happening somewhere, or
you have too large or too many local arrays or variables, or
you are using alloca to much.

Are you sure your page size is 4MB, not 4KB? It will be in bytes I think (4096 = 4K).

Share this post


Link to post
Share on other sites
If you're using a library that originated on UNIX-style systems, and uses lots of alloca(), then you may need to increase your stack size. ODE (the Open Dynamics Engine) is one such library. Note that alloca() is an order of magnitude faster for lots of small allocations than malloc(), so if you can live with the limitations of alloca(), it's a good optimization.

You _can_ usually re-code programs that use alloca() to use something like a linear allocator; or just use malloc() and insert free() in all exit paths of your function, but beware that there may be a slowdown.

The stack size on all x86-32 systems is 4 kB (4096 bytes).

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
The stack size on all x86-32 systems is 4 kB (4096 bytes).


That's the page size in protected mode. Under Windows NT/2k/Xp there are sections of kernel memory with 4Mb page sizes - but that's not something that a game/applications programmer should be concerned with.

It's possible to recover from a blown stack - see: Ask Dr. GUI #49: You've Blown Your Stack. However, note that the code in this article is geared towards recovering for a graceful exit and not to continue running as if nothing happened.

Share this post


Link to post
Share on other sites
Well,sorry you're right it's 4K(4096B)as you said.
Thanks for the tips,i wasn't going to change my stack size anyway but try to fix the overflow problem my recursion is too big i guess,it tries to use 2MB of stack.So i have to keep it short.Thanks if you have some more articles concerning this problem i would very much appreciate.

Thanks once again for your help.

Share this post


Link to post
Share on other sites
You might be able to continue using a recursive algorithm if you can reduce the amount of stack needed by each recursive call. Here are some questions to ask about your code: Can the number of local variables used by the function be reduced? Is there any redundancy in the function that can be eliminated? Are each of the parameters to the function absolutely necessary? Can the overall problem be broken down into smaller pieces and computed separately in a way that provides for combining those sub results into a final result?

I think a complete discussion of recursion removal would require a separate thread. That said, it often boils down to replacing recursive calls with a "push" of the function arguments onto a local array patterned after a stack (lifo - last in first out) and a "pop" to remove them and operate on them. Other situations require replacing recursive calls with a "put" of the function arguments onto a local array patterned after a "queue" (fifo - first in first out) and a "get" to remove them and operate on them.

Share this post


Link to post
Share on other sites
Well ,i've just folowed your advice ,and used a less deeper recursion and it works fine now.I used a sepparate function that reccurses less.

Thanks for all your support.

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