深入理解BSS(Block Started by Symbol)

本文详细解释了ELF格式中的BSS段概念及其作用。BSS段主要用于存放未初始化的全局变量和静态变量,这些变量不需要实际磁盘空间。文章还介绍了BSS段如何节省空间,并列举了不同类型变量存放的具体示例。

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

理解ELF的BSS section, 可以概括为:

  • Uninitialized global/static data
  • "Block Started by Symbol"
  • "Better Save Space"
  • Has section header but occupies no space

CSAPP一书对bss的描述如下:

.bss: 未被初始化的全局的C变量。这一节在o文件中不占实际的空间,只是一个place holder。o文件格式之所以区分初始化的变量和未被初始化的变量是因为处于空间利用率上的考虑。没有被初始化的变量确实没有必要占用实际的磁盘空间。

关于"Better Save Space", 维基百科上是这么说的, (原文点这里

Peter van der Linden, a C programmer and author, says, "Some people like to
remember it as 'Better Save Space.' Since the BSS segment only holds variables
that don't have any value yet, it doesn't actually need to store the image of
these variables. The size that BSS will require at runtime is recorded in the
object file, but BSS (unlike the data segment) doesn't take up any actual space
in the object file."

另外, 在Memory Layout of C Programs一文中,对BSS的描述如下:

Uninitialized data segment, often called the “bss” segment, named after an 
ancient assembler operator that stood for “block started by symbol.” Data 
in this segment is initialized by the kernel to arithmetic 0 before the program 
starts executing

uninitialized data starts at the end of the data segment and contains all global 
variables and static variables that are initialized to zero or 
do not have explicit initialization in source code.

For instance a variable declared static int i; would be contained in the BSS segment.
For instance a global variable declared int j; would be contained in the BSS segment.

这里明确了那些初始化为0的全局变量和静态变量也是被保存在bss中。 也就是说,bss包含:

  • 所有未被显示地初始化的全局变量和静态变量
  • 所有被显示地初始化为0的全局变量和静态变量
1 If a variable declared static int i;     it would be contained in the BSS segment.
2 If a global variable declared int j;     it would be contained in the BSS segment.
3 If a variable declared static int i = 0; it would be contained in the BSS segment.
4 If a global variable declared int j = 0; it would be contained in the BSS segment.

转载于:https://www.cnblogs.com/idorax/p/6400210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值