Can anyone please tell me the utility of .bss section. (stores uninitialised data)..but why can't it be stored in Data segment (together with other initialised data)?
In a ROM based system, when you put a variable in the Data (initialized data) segment, it requires rom storage for the initialization value and address. It also requires the C startup code to take the time to initialize the RAM location. For scratch buffers and maybe the heap (if used) this can be a lot of wasted space and time.
In a disk based system, it still effects the image size and startup time of a program.
Normally the un-initialized storage still gets set to 0 by the startup code, but that can be removed by the person writing the startup code (crt0.s or crt0.c).
It doesn't. It stores variables that don't have an explict initializer given by your code, but which are nevertheless initialized (to all-bits-zero). It may even hold those that do have an explicit initializer --- if that initializer is zero, your platform a sane one, and your compiler a clever little bastard.
It could. But that would be rather spectacularly wasteful. There's no good reason to a store lots of zero-bytes as part of the program, when you can just have the startup code to the equivalent of a
memset(start_bss, 0, length_bss)
instead.
Basically, the .bss segment is a poor man's initialiser data compression algorithm.
--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
The difference is that a const is initialized with a specified value, which is stored in ROM. So the ROM contains a copy of every const variable. This takes up space. A non inilialized variable is simply cleared on startup. There is no copy in ROM for that, the startup code simply clears all the variables in the .bss segment to 0 in a loop. That is, when you do not modify this behaviour by changing the startup code. In many embedded systems, this clearing is skipped because such a system would have to recover from a reset without clearing the entire state of the device. You as the programmer are then responsible for clearing these variables, if necessary.
That is just a peculiarity of the C-language standard that require such initialisation, however, many languages do not make any assumption about the initial value. A proper linker input (object) file specification should be source language independent.
It is up to the C-compiler to assign addresses into the data segment and the C-language initialisation code to clear the data segment at startup, move any non-zero initial values to the data segment before starting the main() function.
That's contradictory. A variable of static storage duration is
*always* initialized, regardless of whether it came with an explicit initializer in its definition or not.
So the difference is not whether it's initialized or not, it's what the initial value is. Sometimes the initialization can be done by a simple clearing of all bits to zero, sometimes it can't. If it can, it's advantageous to do so.
--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.