Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How big is the stack memory for a certain program, and are there any compiler flags that can set it?

As the title states: Is there any general "rule of thumb" about the size of the stack. I'm guessing the size will vary depending on the OS, the architecture, the size of the cache(s), how much RAM is available etc.

However can anything be said in general, or is there any way to find out, how much of the stack, this program is allowed to use?. As a bonus question is there any way (with compiler flags etc. (thinking mostly C/C++ here, but also more general)) that the size of the stack can be set to a fixed size by the user?

Btw, I'm asking strictly out of curiosity, I'm not having a stack overflow. :)

like image 706
Andersnk Avatar asked Mar 11 '13 00:03

Andersnk


2 Answers

In Windows the default stack size for a thread is a million bytes, regardless of operating system, etc.

In managed code (C#, VB, etc) you can force a new thread to have a different stack size with this ctor:

http://msdn.microsoft.com/en-us/library/5cykbwz4.aspx

To change the stack size of the default thread of a Windows program, whether it is managed or not, you can use the editbin utility:

http://msdn.microsoft.com/en-us/library/xd3shwhf.aspx

like image 196
Eric Lippert Avatar answered Oct 05 '22 10:10

Eric Lippert


Yes you can set the stack size, it usually is a linker flag, and it depends on your toolchain (typically this is referred to by the name of the compiler).

  • For Microsoft Visual C++, use the /F option to change the size, and DUMPBIN /HEADERS to see what the setting is.
  • For the GCC toolchain and most other Unix linkers, use -Wl,--stack

You will also find several existing questions here on StackOverflow.

like image 27
Ben Voigt Avatar answered Oct 05 '22 09:10

Ben Voigt