现在的位置: 首页 > 综合 > 正文

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

2013年11月30日 ⁄ 综合 ⁄ 共 3912字 ⁄ 字号 评论关闭
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                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex 
    960     248      12    1220     4c4 a.out             960     248       8    1216     4c0 
论1:未初始化的全局变量保存在BSS段    
-------------------------------
[root@pc201 ccodes]# cat test.c
int g_data=1;
int main()
{
 return 1;
}
[root@pc201 ccodes]# size a.out                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex 
    960     252       8    1220     4c4 a.out             960     248       8    1216     4c0 
论2:经过初始化的全局变量保存在数据段中    
-------------------------------  
[root@pc201 ccodes]# cat test.c
int main()
{
 static int g_data;
 return 1;
}
[root@pc201 ccodes]# size a.out                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex
    960     248      12    1220     4c4 a.out             960     248       8    1216     4c0
论3:未初始化的静态变量保存在BSS段中
--------------------------------  
[root@pc201 ccodes]# cat test.c
int main()
{
 static int g_data=1;
 return 1;
}
[root@pc201 ccodes]# size a.out                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex 
    960     252       8    1220     4c4 a.out             960     248       8    1216     4c0 
论4:经过初始化的静态变量保存在数据段中   
-------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
 int i_data=1;
 return 1;
}
[root@pc201 ccodes]# size a.out                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex 
    976     248       8    1232     4d0 a.out             960     248       8    1216     4c0 
论5:函数内部声明的局部变量保存在堆栈段(text)     
------------------------------
[root@pc201 ccodes]# cat test.c
const int g_data=1;
int main()
{
 return 1;
}
[root@pc201 ccodes]# size a.out                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex 
    964     248       8    1220     4c4 a.out             960     248       8    1216     4c0 
论6:const修饰的全局变量保存在文本段
------------------------------
[root@pc201 ccodes]# cat test.c
int main()
{
 const int i_data=1;
 return 1;
}
[root@pc201 ccodes]# size a.out                      [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename        text    data     bss     dec     hex 
    976     248       8    1232     4d0 a.out            960     248       8    1216     4c0 
结论7:const修饰的局部变量保存在堆栈段
------------------------------
[root@pc201 ccodes]# cat test.c
char *pstr="";
int main()
{
 return 1;
}
[root@pc201 ccodes]# size a.out                       [root@pc201 ccodes]# size a.out                
   text    data     bss     dec     hex filename         text    data     bss     dec     hex 
    961     252       8    1221     4c5 a.out             960     248       8    1216     4c0 
[root@pc201 ccodes]# cat test.c
char *pstr="123456789";
int main()
{
 return 1;
}
[root@pc201 ccodes]# size a.out                    [root@pc201 ccodes]# size a.out                 
   text    data     bss     dec     hex filename       text    data     bss     dec     hex 
    970     252       8    1230     4ce a.out           960     248       8    1216     4c0 
以发现,前后数据段和BSS段大小均未发生变化,而文本段增加了9个字节。    
论8:字符串常量保存在文本段
结论
1、经过初始化的全局变量和静态变量保存在数据段中。
2、未经初始化的全局变量和静态变量保存在BSS段。
3、函数内部声明的局部变量保存在堆栈段中。
4、const修饰的全局变量保存在文本段中,const修饰的局部变量保存在堆栈段中。          
5、字符串常量保存在文本段中。

抱歉!评论已关闭.