我能在C / c++中得到堆栈的极限吗?

时间:2022-09-01 20:48:04

My question is pretty simple and straightforward: if I have e.g. 1MB of RAM assigned to the program's stack, can I get the addresses of the start and the end, or the start and the length?

我的问题非常简单和直接:如果我有1MB的内存分配给程序堆栈,我能得到开始和结束的地址,或者开始和长度的地址吗?

I'm using Visual Studio 2013.

我使用的是Visual Studio 2013。

3 个解决方案

#1


30  

You should question your assumptions about stack layout.

您应该质疑您对堆栈布局的假设。

Maybe the stack doesn't have just one top and bottom

也许这个堆栈没有一个顶部和底部

Maybe it has no fixed bottom at all

也许它根本没有固定的底部

Clearly there's no portable way to query concepts which are not portable.

显然,没有可移植的方法来查询不可移植的概念。

From Visual C++, though, you can use the Win32 API, depending on Windows version.

不过,在Visual c++中,您可以根据Windows版本使用Win32 API。

On Windows 8 it is very easy, just call GetCurrentThreadStackLimits

在Windows 8上,这很简单,只需调用getcurrentthreadstacklimit

Earlier versions need to use VirtualQueryEx and process the results somewhat. Getting one address in the stack is easy, just use & on a local variable. Then you need to find the limits of the reserved region that includes that address. Joe Duffy has written a blog post showing the details of finding the bottom address of the stack

早期的版本需要使用VirtualQueryEx并处理结果。在堆栈中获取一个地址很容易,只需使用&在局部变量上。然后您需要找到包含该地址的保留区域的限制。Joe Duffy写了一篇博文,展示了找到堆栈底部地址的细节。

#2


9  

GetCurrentThreadStackLimits seems to do what you're looking for, getting the lower/upper boundaries of the stack into pointer addresses:

getcurrentthreadstacklimit似乎做了你想做的事情,将堆栈的下/上边界设置为指针地址:

ULONG_PTR lowLimit;
ULONG_PTR highLimit;
GetCurrentThreadStackLimits(&lowLimit, &highLimit);

Looks like it is only available on Windows 8 and Server 2012 though.

看起来它只能在Windows 8和Server 2012上使用。

Check the MSDN

查看MSDN

#3


0  

On Windows before 8, implement GetCurrentThreadStackLimits() yourself:

在Windows 8之前,自己实现GetCurrentThreadStackLimits():

#include <windows.h>
#if _WIN32_WINNT < 0x0602
VOID WINAPI GetCurrentThreadStackLimits(LPVOID *StackLimit, LPVOID *StackBase)
{
    NT_TIB *tib = (NT_TIB *) NtCurrentTeb();
    *StackLimit = tib->StackLimit;
    *StackBase = tib->StackBase;
}
#endif

#1


30  

You should question your assumptions about stack layout.

您应该质疑您对堆栈布局的假设。

Maybe the stack doesn't have just one top and bottom

也许这个堆栈没有一个顶部和底部

Maybe it has no fixed bottom at all

也许它根本没有固定的底部

Clearly there's no portable way to query concepts which are not portable.

显然,没有可移植的方法来查询不可移植的概念。

From Visual C++, though, you can use the Win32 API, depending on Windows version.

不过,在Visual c++中,您可以根据Windows版本使用Win32 API。

On Windows 8 it is very easy, just call GetCurrentThreadStackLimits

在Windows 8上,这很简单,只需调用getcurrentthreadstacklimit

Earlier versions need to use VirtualQueryEx and process the results somewhat. Getting one address in the stack is easy, just use & on a local variable. Then you need to find the limits of the reserved region that includes that address. Joe Duffy has written a blog post showing the details of finding the bottom address of the stack

早期的版本需要使用VirtualQueryEx并处理结果。在堆栈中获取一个地址很容易,只需使用&在局部变量上。然后您需要找到包含该地址的保留区域的限制。Joe Duffy写了一篇博文,展示了找到堆栈底部地址的细节。

#2


9  

GetCurrentThreadStackLimits seems to do what you're looking for, getting the lower/upper boundaries of the stack into pointer addresses:

getcurrentthreadstacklimit似乎做了你想做的事情,将堆栈的下/上边界设置为指针地址:

ULONG_PTR lowLimit;
ULONG_PTR highLimit;
GetCurrentThreadStackLimits(&lowLimit, &highLimit);

Looks like it is only available on Windows 8 and Server 2012 though.

看起来它只能在Windows 8和Server 2012上使用。

Check the MSDN

查看MSDN

#3


0  

On Windows before 8, implement GetCurrentThreadStackLimits() yourself:

在Windows 8之前,自己实现GetCurrentThreadStackLimits():

#include <windows.h>
#if _WIN32_WINNT < 0x0602
VOID WINAPI GetCurrentThreadStackLimits(LPVOID *StackLimit, LPVOID *StackBase)
{
    NT_TIB *tib = (NT_TIB *) NtCurrentTeb();
    *StackLimit = tib->StackLimit;
    *StackBase = tib->StackBase;
}
#endif