7

On Linux, using C, assume I have an dynamically determined n naming the number of elements I have to store in an array (int my_array[n]) just for a short period of time, say, one function call, whereby the called function only uses little memory (some hundred bytes).

Mostly n is little, some tenths. But sometimes n may be big, as much as 1000 or 1'000'000.

How do I calculate, whether my stack can hold n*o + p bytes without overflowing?

Basically: How much bytes are there left on my stack?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • possible duplicate of [Checking available stack size in C](http://stackoverflow.com/questions/53827/checking-available-stack-size-in-c) – Brendan Long Dec 03 '11 at 04:30
  • @BrendanLong, I saw that question, but as it states “I'm using MinGW with GCC 3.4.5 (mingw-special vista r3)”, the answers are somewhat Windows centered. My question is Linux/*nix centered. ;-) – Kijewski Dec 03 '11 at 04:35
  • why can't you use `getrusage()` and `getrlimit()`? – sverre Dec 03 '11 at 04:50
  • @sverre, I'd be happy if you explain getrusage and getrlimit to me. The man page of [getrusage](http://www.kernel.org/doc/man-pages/online/pages/man2/getrusage.2.html) says " ru_isrss (unmaintained) This field is currently unused on Linux". – Kijewski Dec 03 '11 at 04:57
  • @kay - Look at the 4th response on that page. – Brendan Long Dec 03 '11 at 05:05
  • @Brendan Long: The 4th response has the same score as two other responses and their order is randomized. Thus, "the 4th response on that page" does not uniquely name a response. Use a link? – Thanatos Dec 03 '11 at 06:51
  • http://stackoverflow.com/questions/53827/checking-available-stack-size-in-c#answer-59579 or http://stackoverflow.com/questions/53827/checking-available-stack-size-in-c#answer-5864773 – Brendan Long Dec 03 '11 at 21:51

2 Answers2

4

Indeed, the checking available stack question gives good answer.

But a more pragmatic answer is: don't allocate big data on the call stack.

In your case, you could handle differently the case when n<100 (and then allocating on the stack, perhaps thru alloca, makes sense) and the case when n>=100 (then, allocate on the heap with malloc (or calloc) and don't forget to free it). Make the threshold 100 a #define-d constant.

A typical call frame on the call stack should be, on current laptops or desktops, a few kilobytes at most (and preferably less if you have recursion or threads). The total stack space is ordinarily at most a few megabytes (and sometimes much less: inside the kernel, stacks are typically 4Kbytes each!).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
4

If you are not using threads, or if you know that your code executes on the main stack, then

  1. Record current stack pointer when entering main
  2. In your routine, get current stack limit (see man getrlimit)
  3. Compare difference between current stack pointer and the one recorded in step 1 with the limit from step 2.

If you are using threads and could be executing on a thread other than main, see man pthread_getattr_np

Employed Russian
  • 199,314
  • 34
  • 295
  • 362