函数中_beginthread , 第二个参数 (stack_size) 是什么意思?
堆栈大小在哪里?默认值 (0) 是什么意思?
最佳答案
堆栈大小在哪里?
调用栈是一个stack维护有关执行软件的事件函数调用的信息。它也称为执行堆栈、控制堆栈或运行时堆栈。在多线程软件中,每个线程都有自己的调用栈。
调用堆栈的主要目的是通过跟踪每个函数调用返回的位置来管理控制流。当进行函数调用时,一个新的 stack frame被压入该函数的堆栈。当函数返回时,它的栈帧被弹出,控制流返回到调用者下一条指令的地址。
栈帧通常包括:
- 将地址返回给调用者
- 传递给函数的参数
- 保存的寄存器和局部变量
参数也可以通过 CPU 寄存器传递,但这有缺点(即参数数量有限,计算可能需要寄存器。)
同样,并非所有局部变量都必须分配到当前堆栈帧上。支持 closures 的语言要求自由变量在函数返回后继续存在,但当当前堆栈帧弹出并将控制权返回给调用者时,调用堆栈上的局部变量将被释放。
我的观点是局部变量的参数传递和分配是由语言和编译器实现决定的;你不应该假设它们总是存在于堆栈中。
stack_size 是什么意思?默认值0是什么?
来自MSDN documentation on _beginthread ,位于备注部分:
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.
关于c - _beginthread 的第二个参数 stack_size 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538008/