Why and how much stackk size to give in CreateThread api

Hi,

I am trying to learn thread programming using C++ and Win32 thread API.

CreateThread() api takes DWORD dwStackSize as it second parameter, and if we pass NULL then default of 1MB will be assigned.

Can anyone please explain the importance and guideline for this parameter.

Also if a process is having 3 thread then there will be three different stack for these thread. And when we create a process then this process has one primary thread. I think the size of this thread is provided some where?? Not knowing how and where?

Advice related to thread using win32 will be greatly appreciated.

Thanks
The stack in Windows grows dynamically, you should use zero to specify that's what what you want.

The stack is where the language stores the return contexts of function calls. Local variables are stored on the stack. You should use as little as possible, but you can't tell within the program if you're close to running out, so you have to err on the side of caution.
Also if a process is having 3 thread then there will be three different stack for these thread. And when we create a process then this process has one primary thread. I think the size of this thread is provided some where?? Not knowing how and where?


The stack size is embedded as two values(reserve & commit) in the executable file and is set using a linker switch. The default is 1MB reserved from the paging file with 4KB(1 page) committed to physical memory. If you pass anything other than 0 as dwStackSize the new thread will use that value only if it is larger than the reserve value stored in the executable.
Last edited on
When will stackoverflow occur?
When will stackoverflow occur?
When you've used it all.

As I said before, you can't tell within the program when you're close to running out.

It's a real problem. Some modern OS' allow the stack to grow the stack to some upper limit. In general, you should treat the stack as a scarce resource. That is, be careful with recursion, don't declare too many or too large objects on the stack.
Topic archived. No new replies allowed.