1

using GCC (windows 32, C only, default build options ).

what is the largest size of my local stack.. ?

for example:

void myfunc (void)
{
char is_this_too_big_for_a_local_var[64*1024*1024];

somefunc(is_this_too_big_for_a_local_var);
}

possibly from some traumatic experience during my early days of coding, I've never attempted to ever use more than at most 16k local storage.. ( yes, habits picked up during the days of 286's, 16bit computers)..

Anyway, is locating really large variables locally OK ? or is a few K still the upper limit ?

1 Answers1

2

Even if your system has a large stack, you should not get in a habit of using it. Code with large stack frames becomes problematic as soon as you want to use it in multi-threaded programs, because each thread's stack needs to be large enough to accommodate the largest possible stack usage, and the code creating the thread will want to use pthread_attr_setstacksize (or equivalent) to avoid spending more virtual address space and commit charge than necessary on each thread.

Edit: Here's an idea for a "portable" (to POSIX systems) way to ensure you have the space you want on your main thread's stack:

  1. At the beginning of main, copy argv to newly allocated memory.
  2. Then create a new thread with the desired stack size, and pass it the copy of argv. This will fail and report the error if the desired size is not available.
  3. In the new thread, re-invoke main with the new argv, but with a global flag set to avoid repeating steps 1/2.
  4. pthread_exit from the initial "main thread" and treat your new thread as the "main thread".
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711