I'm using MinGW with GCC 3.4.5 (mingw-special vista r3).
My C application uses a lot of stack so I was wondering is there any way I can tell programatically how much stack is remaining so I can cleanly handle the situation if I find that I'm about to run out.
If not what other ways would you work around the problem of potentially running out of stack space?
I've no idea what size of stack I'll start with so would need to identify that programatically also.
Raymond Chen (The Old New Thing) has a good answer to this sort of question:
Here's some Win32 details on stack allocation: MSDN.
If you think you might be limited by stack space, you will almost certainly be limited by available virtual memory, in which case, you will need to find a different solution.
What exactly are you trying to do?
For windows: I've done this before using the VirtualQuery function from Kernel32.dll. I only have an example in C# but it demonstrates the technique:
BTW: This code can also be found on StackOverflow on another question which I asked when I was trying to fix a bug in the code: Arithmetic operation resulted in an overflow in unsafe C#enter link description here
The getrusage function gets you the current usage . (see
man getrusage
).The
getrlimit
in Linux would help fetching the stack size with theRLIMIT_STACK
parameter.Please give a look at
man getrlimit
. The same information could be fetched byulimit -s
orulimit -a
stack size row. Also have a look atsetrlimit
function which would allow to set the limits. But as the mentioned in the other answers if you need to adjust stack then probably you should re consider your design. If you want a big array why not take the memory from the heap ?Assuming you know the size of the full stack you could probably add some assembly code to read ESP.
If you read ESP and save it aside in the main function you can compare the current ESP to the ESP you have in main and see how much ESP has changed. That'll give you an indication of how much stack you're used.
On Linux, you would call getrusage and check the returned struct rusage's ru_isrss member (integral unshared stack size).
From the MINGW site and its sourceforge site's tracking of patches, I see that in May of 2008 there was some patching done around getrusage and it looks like it's been generally supported for quite a while. You should check carefully for any caveats in terms of how much of the typical Linux functionality is supported by MinGW.
Taking the address of a local variable off the stack would work. Then in a more nested call you can subtract the address of another local to find the difference between them
If you code is multi-threaded then you need to deal with storing the top_of_stack variable on a per-thread basis.