In windows (or any other OS for that matter) what determines how much stack I can use? The name of this very website makes me assume it's possible to run out of stack so should I avoid putting large amounts of data on the stack?
It is language specific, Compiler specific and probably OS specific, but you should put large amount of data on the heap and not on the stack.
There are ways to change the stack size - but I wouldn't mess with it !
If you want to know your stack size using trial and error - just create an array on the stack and see how much it lets you...
Its completely OS specific and configurable, on linux you can check and change with the ulimit call in the shell.
Depends on what you call large, my current Debians standard stack size is 8 megs, which is large enough to have large arrays of 1Meg for example.
On Win32 the default stack size is 1MB, it can be adjusted when calling CreateThread() and in the compiler settings.
The OS will set an upper bound, but you can set a specific limit with your linker, which usually has a specific default, often quite lower than what the OS will allow.
You can set the stack size for your application in Visual Studio under
Project->Properties->Linker->System
Although not recommended a a programing technique, its fairly simple to retrieve the amount of free stack space:
CONTEXT Context;
memset(&Context, 0, sizeof(Context));
RtlCaptureContext(&Context);
long stackFree = Context.Rsp;
On Windows, for a native C/C++ project in Visual Studio, the stacksize for the initial/main thread is set using the linker's /STACK
option ("Linker/System/Stack Reserve Size" in the IDE's project properties), and defaults to 1MB. This is also the default thread stack size for new threads that don't specify something more specific.
For subsequently spawned threads, _beginthread()
, _beginthreadex()
and CreateThread()
all have a parameter to let you specify the stack size for a thread, which will default to what you set in the linker properties if you pass in zero.
See http://msdn.microsoft.com/en-us/library/ms686774.aspx for details.