用size命令分析linux程序内存段的分布


Size命令的输出不包括stack和heap的部分。只包括文本段(text), 代码段(data),未初始化数据段(bss)三部分。

1、文本段:包含程序的指令,它在程序的执行过程中一般不会改变。
2、数据段:包含了经过初始化的全局变量和静态变量,以及他们的值。
3、BSS段:包含未经初始化的全局变量和静态变量。
4、堆栈段:包含了函数内部声明的局部变量。当然,上面段的作用不仅于此,具体的作用

int main()
{
  return 1;
}

[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out              
-------------------------------     
      

[root@pc201 ccodes]# cat test.c
int g_data;
int main()
{
  return 1;
}


[root@pc201 ccodes]# size a.out                     
     text      data       bss       dec       hex filename             
      960       248         12      1220       4c4 a.out  


原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 

              
论1:未初始化的全局变量保存在BSS段  


  
-------------------------------
[root@pc201 ccodes]# cat test.c
int g_data=1;
int main()
{
  return 1;
}
[root@pc201 ccodes]# size a.out                        
     text      data       bss       dec       hex filename     
      960       252          8      1220       4c4 a.out     


原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 

        
论2:经过初始化的全局变量保存在数据段中  



  
-------------------------------  
[root@pc201 ccodes]# cat test.c
int main()
{
  static int g_data;
  return 1;
}
[root@pc201 ccodes]# size a.out                   
     text      data       bss       dec       hex filename       
      960       248         12      1220       4c4 a.out 


原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 


论3:未初始化的静态变量保存在BSS段中





--------------------------------  
[root@pc201 ccodes]# cat test.c
int main()
{
  static int g_data=1;
  return 1;
}
[root@pc201 ccodes]# size a.out                     
     text      data       bss       dec       hex filename           
      960       252          8      1220       4c4 a.out       


原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 


    
论4:经过初始化的静态变量保存在数据段中   





-------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
  int i_data=1;
  return 1;
}
[root@pc201 ccodes]# size a.out                         
     text      data       bss       dec       hex filename     
      976       248          8      1232       4d0 a.out       

 
原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 

 
论5:函数内部声明的局部变量保存在堆栈段(text)  




    
------------------------------
[root@pc201 ccodes]# cat test.c
const int g_data=1;
int main()
{
  return 1;
}
[root@pc201 ccodes]# size a.out                   
     text      data       bss       dec       hex filename          
      964       248          8      1220       4c4 a.out       



原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 

       
论6:const修饰的全局变量保存在文本段






------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
  const int i_data=1;
  return 1;
}
[root@pc201 ccodes]# size a.out                  
     text      data       bss       dec       hex filename       
      976       248          8      1232       4d0 a.out    


原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 


             
结论7:const修饰的局部变量保存在堆栈段



------------------------------
[root@pc201 ccodes]# cat test.c
char *pstr="";
int main()
{
  return 1;
}
[root@pc201 ccodes]# size a.out                       
     text      data       bss       dec       hex filename          
      961       252          8      1221       4c5 a.out          

[root@pc201 ccodes]# cat test.c
char *pstr="123456789";
int main()
{
  return 1;
}
[root@pc201 ccodes]# size a.out                     
     text      data       bss       dec       hex filename       
      970       252          8      1230       4ce a.out    


原始内存分布:
[root@pc201 ccodes]# size a.out 
     text      data       bss       dec       hex filename
      960       248          8      1216       4c0 a.out 



 
以发现,前后数据段和BSS段大小均未发生变化,而文本段增加了9个字节。     
论8:字符串常量保存在文本段

结论
1、经过初始化的全局变量和静态变量保存在数据段中。
2、未经初始化的全局变量和静态变量保存在BSS段。
3、函数内部声明的局部变量保存在堆栈段中。
4、const修饰的全局变量保存在文本段中,const修饰的局部变量保存在堆栈段中。          
5、字符串常量保存在文本段中。












本文转自:

http://blog.sina.com.cn/s/blog_53fab15a01019lbq.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值