内存布局练习 C++

Memory layout exercise C++

我正在阅读 B.Stroustrup 的“使用 C++ 的编程原则和实践”,并且我正在尝试完成大部分练习。这个很有趣,我不知道从哪里开始。

练习是:

Consider the layout in 17.4. Write a program that tells the order in which static storage, the stack, and the free store are laid out in memory. In which direction does the stack grow : upward toward higher addresses or downward toward lower addresses?

17.4 中的布局:

Code - memory for code

Static data - space for global variables

Free store - available memory

Stack - memory for calling functions and space for their arguments and local variables

我怎么才能真正知道这一切发生的顺序?更好的是,我如何实际输出它发生时发生的顺序?

它不是应该由编译器处理,还是我误解了这个练习?一些小技巧真的很酷!

谢谢

Isn't it supposed to be handled by compiler

链接器,更具体地说。

这是一个起点,完全按照 Joseph Mansfield 的建议进行。

#include <iostream>

int static_data;

int main()
{
    int stack_data;

    if ( &static_data < &stack_data )
    {
        std::cout << "Static data is in lower memory than the stack.";
    }
}