Linux--验证内存地址空间排布

Linux--验证内存地址空间排布_第1张图片

 验证:

#include
#include
 
int g_val1 = 10;
int g_val2 = 20;
 
int g_val3;
int g_val4;
 
int main(int argc, char* argv[], char* env[])
{
  //任务:验证进程地址空间
  
  //代码区
  printf("code address:%p\n", main);
  
  //只读常量区
  const char* p1 = "hello";
  printf("only read:%p\n", p1);
  const char* p2 = "hello world";
  printf("only read:%p\n", p2);
 
  //全局区
  printf("init global g_val1:%p\ninit global g_val2:%p\n", &g_val1, &g_val2);
  printf("uninit global g_val3:%p\nuninit global g_val4:%p\n", &g_val3, &g_val4);
 
  //堆区
  char* tmp1 = (char*)malloc(10);
  char* tmp2 = (char*)malloc(10);
  char* tmp3 = (char*)malloc(10);
  char* tmp4 = (char*)malloc(10);
  printf("heap:%p\n", tmp1);
  printf("heap:%p\n", tmp2);
  printf("heap:%p\n", tmp3);
  printf("heap:%p\n", tmp4);
 
  //栈区
  printf("stack:%p\n", &tmp1);
  printf("stack:%p\n", &tmp2);
  printf("stack:%p\n", &tmp3);
  printf("stack:%p\n", &tmp4);
 
  //命令行参数
  for(int i = 0; i < argc; i++)
  {
    printf("argv[%d]:%p\n", i, argv[i]);
  }
  //环境变量
  for(int i = 0; env[i]; i++)
  {
    printf("env[%d]:%p\n", i, env[i]);
  }
  return 0;
}

Linux--验证内存地址空间排布_第2张图片

 

你可能感兴趣的:(Linux,linux)