简体   繁体   English

堆栈内存地址区

[英]heap stack memory address area

I am currently trying to get in touch again with C, memory usage and assembly.我目前正在尝试再次与 C、内存使用和汇编取得联系。 I am working on an Ubuntu(64-bit) VM.我正在使用 Ubuntu(64 位)VM。 Given the code bellow鉴于以下代码

#include <stdio.h>

int global_var;
int global_init_var=5;

void function(){
    int stack_var;
    printf("functions stack_var is at",&stack_var);
}

int main(){
    int stack_var;
    static int static_init_var=5;
    static int static_var;
    int *heap_var_ptr;

    printf("global_init_var is at adress 0x%08x\n",&global_init_var);
    printf("static_init_var is at adress 0x%08x\n",&static_init_var);
    printf("static_var is at adress 0x%08x\n",&static_var);
    printf("global_var is at adress 0x%08x\n",&global_var);
    printf("heap_var_ptr is at adress 0x%08x\n",heap_var_ptr);
    printf("stack_var is at adress 0x%08x\n",&stack_var);
    function();
}

I get the output:我得到输出:

global_init_var is at 0x00601040 global_init_var 在 0x00601040

static_init_var is at 0x00601044 static_init_var 位于 0x00601044

static_var is at 0x0060104c static_var 位于 0x0060104c

global_var is at 0x00601050 global_var 在 0x00601050

heap_var_ptr is at 0xb9f8dd00 heap_var_ptr 位于 0xb9f8dd00

stack_var is at 0xb9f8dcfc stack_var 在 0xb9f8dcfc

functions stack_var is at 0xb9f8dcd4函数 stack_var 位于 0xb9f8dcd4

my question is: Why is the stack adress area the same as the heap address area?我的问题是:为什么栈地址区和堆地址区一样?

Your heap pointer variable inside main: ' int *heap_var_ptr; main 中的堆指针变量:' int *heap_var_ptr; ' '
It will be allocated in stack only (4 bytes in general).它将仅在堆栈中分配(通常为 4 个字节)。
And getting the address as in your code :并获取您的代码中的地址:
printf("heap_var_ptr is at adress 0x%08x\\n",heap_var_ptr); printf("heap_var_ptr 位于地址 0x%08x\\n",heap_var_ptr);
This will only get you address of some garbage stored inside 'heap_var_ptr'这只会为您提供存储在“heap_var_ptr”中的一些垃圾的地址

If you really want to print address of heap and see the difference, modify your program with below lines:如果您真的想打印堆地址并查看差异,请使用以下几行修改您的程序:

  1. Allocate some address using malloc/alloc and assign the pointer of allocated memory to your variable:使用 malloc/alloc 分配一些地址并将已分配内存的指针分配给您的变量:
    int *heap_var_ptr = (int *)malloc(sizeof(int)); int *heap_var_ptr = (int *)malloc(sizeof(int));
    This assigns the allocated heap memory address into your pointer variable stored in stack.这会将分配的堆内存地址分配给存储在堆栈中的指针变量。

  2. Now the address stored in pointer variable heap_var_ptr is from heap.现在存储在指针变量 heap_var_ptr 中的地址来自堆。
    printf("heap_var_ptr is at adress %p\\n",heap_var_ptr); printf("heap_var_ptr 在地址 %p\\n",heap_var_ptr);

You will see the difference in addresses after these changes.您将看到这些更改后地址的差异。


Whereas in your case, you have not used any memory from heap.而在您的情况下,您没有使用堆中的任何内存。
And Hence stack address area is same as of your heap pointer variable.因此堆栈地址区域与您的堆指针变量相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM