我可以在 C/C++ 中获得堆栈的限制吗? [英] Can I get the limits of the stack in C / C++?

查看:34
本文介绍了我可以在 C/C++ 中获得堆栈的限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常简单明了:如果我有例如分配给程序堆栈的 1MB RAM,我可以获取开始和结束的地址,或者开始和长度的地址吗?

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?

我使用的是 Visual Studio 2013.

I'm using Visual Studio 2013.

推荐答案

你应该质疑你对堆栈布局的假设.

You should question your assumptions about stack layout.

也许 堆栈不只有一个顶部和底部

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

显然没有可移植的方式来查询不可移植的概念.

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

不过,在 Visual C++ 中,您可以使用 Win32 API,具体取决于 Windows 版本.

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

在 Windows 8 上很容易,只需调用 GetCurrentThreadStackLimits

On Windows 8 it is very easy, just call GetCurrentThreadStackLimits

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

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

这篇关于我可以在 C/C++ 中获得堆栈的限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆