0

In function _beginthread, what does the second argument (stack_size) mean?

Stack size of where? And what does the default value (0) mean?

Rob
  • 5,223
  • 5
  • 41
  • 62
Amin
  • 87
  • 1
  • 2
  • 7

1 Answers1

3

The stack size of where?

The call stack is a stack that maintains information about active function calls of executing software. It's also known as an execution stack, control stack, or run-time stack. In multi-threaded software, each thread has its own call stack.

The primary purpose of the call stack is to manage control flow by keeping track of where each function call returns to. When a function call is made, a new stack frame is pushed onto the stack for that function. When the function returns, its stack frame is popped off and control flow is returned to the address of the caller's next instruction.

A stack frame typically includes:

  • Return address back to caller
  • Parameters passed to the function
  • Saved registers & local variables

Parameters can also be passed via CPU registers, but there are drawbacks to this (ie. limited number of parameters, & registers may be needed for computation.)

Similarly, all local variables don't have to be allocated on the current stack frame. Languages that support closures require free variables to survive after function return, yet locals on the call stack are deallocated when the current stack frame is popped off and control is returned to the caller.

My point here is that parameter passing and allocation of locals are determined by language and compiler implementation; you shouldn't assume they always exist on the stack.

What does stack_size mean? What is the default value 0?

From the MSDN documentation on _beginthread, found under the Remarks section:

The operating system handles the allocation of the stack when either _beginthread or _beginthreadex is called; you do not need to pass the address of the thread stack to either of these functions.

In addition, the stack_size argument can be 0, in which case the operating system uses the same value as the stack specified for the main thread.

Community
  • 1
  • 1
Rob
  • 5,223
  • 5
  • 41
  • 62
  • 2
    +1 The stack size for the main thread is defined in the PE headers for the executable file. – David Heffernan Sep 24 '11 at 09:03
  • I don't feel like this answers the question, which appears to be posed by someone who either doesn't understand the call stack, or wasn't aware that each thread needs a separate stack. – Ben Voigt Oct 16 '11 at 23:28
  • @BenVoigt: I agree that's relevant information, but I think this does answer the question. The MSDN remarks imply each thread has a stack, and explicitly state it's allocated by the OS. They also explain the usage of the default value 0. Without launching into a broad explanation of Windows threading and stacks, what more should this answer include? I will be glad to edit it in. – Rob Oct 16 '11 at 23:40
  • @robjb: I'd say the "Stack size of where?" requires some explanation that we're talking about the call stack, used to store return addresses and function parameters. – Ben Voigt Oct 17 '11 at 03:40