静态,堆栈和堆内存分配中的地址排序?(Address ordering in static, stack and heap memory allocation?)

编程入门 行业动态 更新时间:2024-10-28 08:29:01
静态,堆栈和堆内存分配中的地址排序?(Address ordering in static, stack and heap memory allocation?)

我正在尝试编写一个程序,显示在静态,堆栈或堆内存上分配的连续变量的地址。

这是我的代码:

#include <iostream> void stack_f() { // array on stack char stack_arr[3] = {'a', 'b', 'c'}; // stack memory addresses std::cout <<"\nStack memory addresses.\n"; for (size_t i = 0; i < 3; ++i) std::cout <<"&stack_arr["<< i <<"] " << reinterpret_cast<void*>(&stack_arr[i]) <<'\n'; } //================================================================================ int main () { // array on static storage static char global_arr[3] = {'a', 'b', 'c'}; // array on heap char *heap_arr = new char[3]; for (size_t i = 0; i < 3; ++i) heap_arr[i] = 'a' + i; // Print addresses of the three arrays // static memory addresses std::cout <<"Static memory addresses.\n"; for (size_t i = 0; i < 3; ++i) std::cout <<"&global_arr["<< i <<"] " << reinterpret_cast<void*>(&global_arr[i]) <<'\n'; // stack memory addresses stack_f(); // heap memory addresses std::cout <<"\nHeap memory addresses.\n"; for (size_t i = 0; i < 3; ++i) std::cout <<"&heap_arr["<< i <<"] " << reinterpret_cast<void*>(&heap_arr[i]) <<'\n'; delete [] heap_arr; getchar(); }

我得到的结果是:

这不是我期望的1 ,因为堆栈上的地址应该从大到小。

我在这里误解了什么?


1.我希望静态和堆内存中的变量地址从小到大的十六进制数增长,而堆栈的地址则以相反的顺序增长。

64位Windows 7,Microsoft Visual C ++ 2010

I am trying to write a program that shows that addresses of consecutive variables allocated either on static, stack or heap memory.

Here is my code:

#include <iostream> void stack_f() { // array on stack char stack_arr[3] = {'a', 'b', 'c'}; // stack memory addresses std::cout <<"\nStack memory addresses.\n"; for (size_t i = 0; i < 3; ++i) std::cout <<"&stack_arr["<< i <<"] " << reinterpret_cast<void*>(&stack_arr[i]) <<'\n'; } //================================================================================ int main () { // array on static storage static char global_arr[3] = {'a', 'b', 'c'}; // array on heap char *heap_arr = new char[3]; for (size_t i = 0; i < 3; ++i) heap_arr[i] = 'a' + i; // Print addresses of the three arrays // static memory addresses std::cout <<"Static memory addresses.\n"; for (size_t i = 0; i < 3; ++i) std::cout <<"&global_arr["<< i <<"] " << reinterpret_cast<void*>(&global_arr[i]) <<'\n'; // stack memory addresses stack_f(); // heap memory addresses std::cout <<"\nHeap memory addresses.\n"; for (size_t i = 0; i < 3; ++i) std::cout <<"&heap_arr["<< i <<"] " << reinterpret_cast<void*>(&heap_arr[i]) <<'\n'; delete [] heap_arr; getchar(); }

The result I get is:

Which is not what I expect1, as addresses on the stack should grow from large to small.

What I've misunderstood here?


1. I'm expecting the addresses of variables located on the static and heap memory to grow from small to large hex numbers, while the addresses of the stack to grow in reverse order.

64-bit Windows 7, Microsoft Visual C++ 2010

最满意答案

你误解的只是当堆栈在x86,x86-64和许多其他现代处理器上向下增长时,数组被分配为单个块,因此增加索引会增加地址。 一旦记住这一点,您的结果就是人们所期望的。

What you've misunderstood is simply that while the stack grows downward on x86, x86-64, and many other modern processors, arrays are allocated as single blocks, and hence increasing indices increase addresses. Once that is borne in mind your results are what one would expect.

更多推荐

本文发布于:2023-08-04 10:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   静态   分配   内存   地址

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!