I know that the BSS segment stores the uninitialized global and static variables and initializes them to zero. But what if the global/static variable is initialized and my second question is I read that BSS segment doesn't consume memory, then where those it store these variables? Thanks
Asked
Active
Viewed 1,594 times
2 Answers
8
You probably read that the BSS segment doesn't consume space in the executable file on disk. When the executable loaded, the BSS segment certainly does consume space in memory. Space is allocated and initialised to zero by the OS loader.

Greg Hewgill
- 951,095
- 183
- 1,149
- 1,285
-
Hey Greg, thanks for replying. What if the global/static variable is initialized, where is that stored? – hue Apr 25 '11 at 06:35
-
1If the initialisation is simple (like just a number), then it's usually stored in the executable (data segment). If the initialisation is more complex (such as a C++ string), then code is run at program startup to initialise the value. – Greg Hewgill Apr 25 '11 at 06:36
-
So I have one more question, so where does the heap is stored. Is it part of the data segment? – hue Apr 25 '11 at 07:02
-
No, the heap is not part of the data segment. The heap is allocated at runtime. – Greg Hewgill Apr 25 '11 at 08:48
-
@GregHewgill So why we need .bss segment,before executable loaded,it's all zero.After executable loaded,the value of these uninitialized global data should be in the heap segment. – Henson Fang Nov 05 '20 at 07:28
2
If initialized, global/static variables are stored in the .DATA segment. When you declare data in the .DATA segment, you provide the values to that data so it would have to be stored as part of the executable.
On the other hand, you only declare how much data you need for the .BSS since you don't need to know what the values are. So if your program declared 2 GB of uninitialized memory, that 2 GB does not contribute to the size of your executable, you won't see it until after it is loaded.

Jeff Mercado
- 129,526
- 32
- 251
- 272
-
Why we need to know how much data we need for the .BSS since we just use it in runtime? – Henson Fang Nov 05 '20 at 07:31
-
When the loader is loading your program, it needs to know how much memory to allocate. That includes memory for the data, bss, and code segments. – Jeff Mercado Nov 06 '20 at 08:28