BSS vs DATA Section: Why Zero Matters
Two memory sections.
Both store global variables.
But only one starts from zero.
1. Program Memory Layout
In systems written in C and C++, global and static variables are placed into specific memory
sections during
compilation.
Two important sections are:
Both contain variables with global or static lifetime, but they differ in how they are initialized at system startup.
Two important sections are:
- DATA section
- BSS section
Both contain variables with global or static lifetime, but they differ in how they are initialized at system startup.
2. The DATA Section
The
Because these variables must begin with predefined data, their initial values are stored in non-volatile memory (such as Flash) and copied into RAM during system startup.
This copy process happens before the program begins normal execution.
DATA section stores global and static variables that have explicit initial
values.Because these variables must begin with predefined data, their initial values are stored in non-volatile memory (such as Flash) and copied into RAM during system startup.
This copy process happens before the program begins normal execution.
3. The BSS Section
The
Instead of storing values in the firmware image, the system simply clears this region to zero during startup.
This approach reduces program size because the binary does not need to store large blocks of zero-initialized data.
BSS section contains global and static variables that are not explicitly
initialized.Instead of storing values in the firmware image, the system simply clears this region to zero during startup.
This approach reduces program size because the binary does not need to store large blocks of zero-initialized data.
4. Why Zero Initialization Matters
The language standards guarantee that global and static variables without explicit initialization
begin with a
value of zero.
To maintain this guarantee, startup code must explicitly clear the BSS region before the program begins execution.
If this step is skipped or implemented incorrectly, variables may start with unpredictable values, leading to unstable system behavior.
To maintain this guarantee, startup code must explicitly clear the BSS region before the program begins execution.
If this step is skipped or implemented incorrectly, variables may start with unpredictable values, leading to unstable system behavior.
5. Practical Insight
Understanding these sections is important when working with:
In embedded firmware, a failure to correctly initialize the DATA and BSS sections can cause system problems before the program even reaches
- linker scripts
- startup code
- memory-constrained embedded systems
- debugging early system initialization
In embedded firmware, a failure to correctly initialize the DATA and BSS sections can cause system problems before the program even reaches
main().