Can I get the limits of the stack in C / C++?

前端 未结 3 2121
走了就别回头了
走了就别回头了 2021-02-12 08:16

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

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-12 08:42

    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.

    On Windows 8 it is very easy, just call GetCurrentThreadStackLimits

    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

提交回复
热议问题