Linux进程的地址空间

1、进程的地址空间

 

 

① 通过打印地址查看数据保存的位置:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int INIT_GLOBAL_VALUE1 = 0;
int INIT_GLOBAL_VALUE2 = 0;
int INIT_GLOBAL_VALUE3 = 0;
int INIT_GLOBAL_VALUE4 = 0;
int INIT_GLOBAL_VALUE5 = 0;
int INIT_GLOBAL_VALUE6 = 0;

int UNINIT_GLOBAL_VALUE1;
int UNINIT_GLOBAL_VALUE2;
int UNINIT_GLOBAL_VALUE3;
int UNINIT_GLOBAL_VALUE4;
int UNINIT_GLOBAL_VALUE5;
int UNINIT_GLOBAL_VALUE6;

int main(int argc,char* argv[],char* env[])
{
    int val_stack = 10;
    int *heap = NULL;
    heap = (int *)malloc(sizeof(int)*5);

    /*打印存储在text代码段的地址*/
    printf("\naddress of printf is %p\n",printf);
    printf("address of  main  is %p\n\n",main);
    
    /*打印存储在.data处的初始化数据*/
    printf("address of INIT_GLOBAL_VALUE1 is %p\n",&INIT_GLOBAL_VALUE1);  
    printf("address of INIT_GLOBAL_VALUE2 is %p\n",&INIT_GLOBAL_VALUE2);
    printf("address of INIT_GLOBAL_VALUE3 is %p\n",&INIT_GLOBAL_VALUE3);
    printf("address of INIT_GLOBAL_VALUE4 is %p\n",&INIT_GLOBAL_VALUE4);  
    printf("address of INIT_GLOBAL_VALUE5 is %p\n",&INIT_GLOBAL_VALUE5);
    printf("address of INIT_GLOBAL_VALUE6 is %p\n\n",&INIT_GLOBAL_VALUE6);

    /*打印存储在.bss处的未初始化数据*/
    printf("address of UNINIT_GLOBAL_VALUE1 is %p\n",&UNINIT_GLOBAL_VALUE1);
    printf("address of UNINIT_GLOBAL_VALUE2 is %p\n",&UNINIT_GLOBAL_VALUE2);
    printf("address of UNINIT_GLOBAL_VALUE3 is %p\n",&UNINIT_GLOBAL_VALUE3);
    printf("address of UNINIT_GLOBAL_VALUE4 is %p\n",&UNINIT_GLOBAL_VALUE4);
    printf("address of UNINIT_GLOBAL_VALUE5 is %p\n",&UNINIT_GLOBAL_VALUE5);
    printf("address of UNINIT_GLOBAL_VALUE6 is %p\n\n",&UNINIT_GLOBAL_VALUE6);    
    
    /*打印堆区*/
    printf("address of heap is %p\n\n",heap);

    /*打印栈区*/
    printf("address of val_stack is %p\n\n",&val_stack);
    free(heap);
    heap = NULL;

    /*打印命令行参数*/
    printf("argv[%d]:%p\n\n",0,argv[0]);

    /*打印环境变量*/
    printf("env[%d]:%p\n",0,env[0]);    

    return 0;
}

 

参考博客:

Linux进程管理---进程地址空间 - 卖寂寞的小男孩 - 博客园 (cnblogs.com)

浅析Linux进程空间布局 - 等不到的口琴 - 博客园 (cnblogs.com)

posted on 2024-05-25 15:50  轩邈、  阅读(24)  评论(0编辑  收藏  举报

导航