Checking available stack size in C

Question :

Checking available stack size in C,

Answer :

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.

Read More  Getting IIS Worker Process Crash dumps


The getrusage function gets you the current usage . (see man getrusage).

The getrlimit in Linux would help fetching the stack size with the RLIMIT_STACK parameter.

#include   int main (void)  {    struct rlimit limit;      getrlimit (RLIMIT_STACK, &limit);    printf ("nStack Limit = %ld and %ld maxn", limit.rlim_cur, limit.rlim_max);  }  

Please give a look at man getrlimit.
The same information could be fetched by ulimit -s or ulimit -a stack size row.
Also have a look at setrlimit 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 ?

That’s the answer Checking available stack size in C, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you