Windows 上的线程堆栈大小 (Visual C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2480095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:09:42  来源:igfitidea点击:

thread stack size on Windows (Visual C++)

windowsstackstack-size

提问by Ziffusion

Is there a call to determine the stack size of a running thread? I've been looking in MSDN thread functions documentation, and can't seem to find one.

是否有调用来确定正在运行的线程的堆栈大小?我一直在查看 MSDN 线程函数文档,但似乎找不到。

采纳答案by El Zorko

Whilst there isn't an API to find out stack size directly, contiguous virtual address space must be reserved up to the maximum stack size - it's just that a lot of that space isn't committed yet. You can take advantage of this and make two calls to VirtualQuery.

虽然没有直接找出堆栈大小的 API,但必须保留连续的虚拟地址空间直到最大堆栈大小 - 只是该空间中有很多尚未提交。您可以利用这一点并对VirtualQuery进行两次调用。

For the first call, pass it the address of any value on the stack to get the base address and size, in bytes, of the committed stack space. On an x86 machine where the stack grows downwards, subtract the size from the base address and VirtualQuery again: this will give you the size of the space reserved for the stack (assuming you're not precisely on the limit of stack size at the time). Summing the two naturally gives you the total stack size.

对于第一次调用,将堆栈上任何值的地址传递给它,以获取已提交堆栈空间的基地址和大小(以字节为单位)。在堆栈向下增长的 x86 机器上,再次从基地址和 VirtualQuery 中减去大小:这将为您提供为堆栈保留的空间的大小(假设您当时没有精确到堆栈大小的限制) )。将两者相加自然会得到总筹码量。

回答by Remus Rusanu

You can get the current committed size from the Top and Bottom in the TEB. You can get the process initial reserve and commit sizes from the PE header. But you cannot retrieve the actual sizes passed to CreateThread, nor is there any API to get the remaining size of reserved nor committed from current stack, see Thread Stack Size.

您可以从TEB的顶部和底部获取当前提交的大小。您可以从PE 标头获取进程初始保留和提交大小。但是您无法检索传递给CreateThread的实际大小,也没有任何 API 可以从当前堆栈中获取保留或提交的剩余大小,请参阅Thread Stack Size