内存布局【代码段、数据段、BSS段、堆、栈】

一、内存布局图

    其中BSS【Block Started by Symbol】意思是:以符号开始的块。

 

内存布局【代码段、数据段、BSS段、堆、栈】

 

 

二、源码【存为mi.cpp】

#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
int bss_var;
int data_var0=1;
int main(int argc, char** argv)
{
  printf("\nbelow are addresses of types of process's mem\n\n");
  printf("----------------------------------------------------------\n");
  printf("Code location:\n");
  printf("\tAddress of main(Code Segment):%p\n",main);
  printf("----------------------------------------------------------\n");
 
  printf("Data Location:\n");
  printf("\tAddress of data_var(Data Segment):%p\n", &data_var0);
  static  int data_var1=2;
  printf("\tNew end of data_var(Data Segment):%p\n", &data_var1);
  printf("----------------------------------------------------------\n");
 
  printf("BSS Location:\n");
  printf("\tAddress of bss_var:%p\n", &bss_var);
  printf("----------------------------------------------------------\n");
 
  char* b = (char*)sbrk((ptrdiff_t)0);
  printf("Heap Location:\n");
  printf("\tInitial end of heap:%p\n", b);
  brk(b+4);
  b=(char*)sbrk((ptrdiff_t)0);
  printf("\tNew  end of heap:%p\n", b);
  char* c=(char*)new char[16];
  printf("\tNew2 end of heap:%p\n", c);
  char* d=(char*)malloc(16);
  printf("\tNew3 end of heap:%p\n", d); 
  b=(char*)sbrk(0);
  printf("\tNew4 end of heap:%p\n", b); 
  printf("----------------------------------------------------------\n");
 
  int stack_var0=3;
  printf("Stack Location:\n");
  printf("\tInitial end of stack:%p\n", &stack_var0);
  int stack_var1=4;
  printf("\tnew end of stack:%p\n",&stack_var1);
  printf("----------------------------------------------------------\n");

  return 0;
}


 

三、编译

[erlang@m176com duan]$ g++ -o mi mi.cpp

 

四、运行

[erlang@m176com duan]$ ./mi

below are addresses of types of process's mem

----------------------------------------------------------
Code location:
        Address of main(Code Segment):0x8048504
----------------------------------------------------------
Data Location:
        Address of data_var(Data Segment):0x8049b7c
        New end of data_var(Data Segment):0x8049b80
----------------------------------------------------------
BSS Location:
        Address of bss_var:0x8049b88
----------------------------------------------------------
Heap Location:
        Initial end of heap:0x85b9000
        New  end of heap:0x85b9004
        New2 end of heap:0x85b9010
        New3 end of heap:0x85b9028
        New4 end of heap:0x85db000
----------------------------------------------------------
Stack Location:
        Initial end of stack:0xbfe24c38
        new end of stack:0xbfe24c34
----------------------------------------------------------

 

五、看一下内存段信息

[erlang@m176com duan]$ ps -efa|grep mi|grep erlang|grep -v grep|awk '{print $2}'
13371
[erlang@m176com duan]$ cat /proc/13371/maps
005eb000-00600000 r-xp 00000000 68:05 2689943    /lib/ld-2.3.4.so
00600000-00601000 r-xp 00015000 68:05 2689943    /lib/ld-2.3.4.so
00601000-00602000 rwxp 00016000 68:05 2689943    /lib/ld-2.3.4.so
00609000-0072e000 r-xp 00000000 68:05 2689944    /lib/tls/libc-2.3.4.so
0072e000-0072f000 r-xp 00124000 68:05 2689944    /lib/tls/libc-2.3.4.so
0072f000-00732000 rwxp 00125000 68:05 2689944    /lib/tls/libc-2.3.4.so
00732000-00734000 rwxp 00732000 00:00 0
00736000-00757000 r-xp 00000000 68:05 2689945    /lib/tls/libm-2.3.4.so
00757000-00759000 rwxp 00020000 68:05 2689945    /lib/tls/libm-2.3.4.so
008b2000-008b9000 r-xp 00000000 68:05 2689948    /lib/libgcc_s-3.4.6-20060404.so.1
008b9000-008ba000 rwxp 00007000 68:05 2689948    /lib/libgcc_s-3.4.6-20060404.so.1
009cd000-00a8d000 r-xp 00000000 68:05 2941513    /usr/lib/libstdc++.so.6.0.3
00a8d000-00a92000 rwxp 000bf000 68:05 2941513    /usr/lib/libstdc++.so.6.0.3
00a92000-00a98000 rwxp 00a92000 00:00 0
08048000-08049000 r-xp 00000000 68:05 6342667    /home/erlang/duan/mi
08049000-0804a000 rw-p 00000000 68:05 6342667    /home/erlang/duan/mi
0996f000-09991000 rw-p 0996f000 00:00 0
b7fc3000-b7fc5000 rw-p b7fc3000 00:00 0
b7fdb000-b7fdc000 rw-p b7fdb000 00:00 0
bff61000-c0000000 rw-p bff61000 00:00 0
ffffe000-fffff000 ---p 00000000 00:00 0

 

六、分页存储管理【是从百度文库中摘录的,在此向作者致敬】

http://wenku.baidu.com/view/b7db25687e21af45b307a899.html

内存布局【代码段、数据段、BSS段、堆、栈】

内存布局【代码段、数据段、BSS段、堆、栈】

内存布局【代码段、数据段、BSS段、堆、栈】
内存布局【代码段、数据段、BSS段、堆、栈】
内存布局【代码段、数据段、BSS段、堆、栈】

内存布局【代码段、数据段、BSS段、堆、栈】



FROM: http://blog.sina.com.cn/s/blog_49f761940100nzr4.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值