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

本文详细解析了程序中不同类型的变量如何被分配到内存的不同区域:文本段、数据段、BSS段和堆栈段。通过一系列示例代码和`size`命令输出分析,阐述了初始化变量、全局变量、静态变量以及局部变量的存储位置,并特别讨论了`const`修饰符对内存布局的影响,以及字符串常量的存放方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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 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 1220 4c4 a.out       960 248 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 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 1220 4c4 a.out       960 248 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 1232 4d0 a.out       960 248 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 1220 4c4 a.out       960 248 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 1232 4d0 a.out       960 248 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 1221 4c5 a.out       960 248 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 1230 4ce a.out 960 248 1216 4c0
以发现,前后数据段和BSS段大小均未发生变化,而文本段增加了9个字节。
论8:字符串常量保存在文本段

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值