what for .bss segment?

Hello Guys,

I am new to this group and embedded domain.

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)?

what is the advantage/ need of .bss segment?

Regards,

Utkarsh.

Reply to
Utkarsh
Loading thread 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).

Good Luck, Bob

Reply to
MetalHead

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.
Reply to
Hans-Bernhard Broeker

Hello,

But from my C (DOS) programming exp. all the static and global variables (that take DS or .bss) are by default initialised to 0.

So from where the case of having "uninitialised data" come from? This really beats me.....

Also, "They" say that it allows effective usage of memory (EEPROM/ RAM etc). Does it make any sense?

Regards,

#define (trying my new nickname :-))

Reply to
#define

const int test = 10;

is initialized and

int test;

isn't.

The former uses ROM to store 10 and the latter doesn't.

Meindert

Reply to
Meindert Sprang

But....

int test;

"test" will be either static or global (to be stored in .bss) with default value 0!!

Is it not initialised with value 0???? if not, then it's fine...but if yes, then i've not got the ans. of my question :-(

i think .bss lands in on-chip EEPROM/ FLASH.

#define

Reply to
#define

Right.

Yes it is.

Everything in the .bss section is usually initialized to 0 by startup code.

You think wrong.

Lame.

--
Grant Edwards                   grante             Yow!  .. I'll make you
                                  at               an ASHTRAY!!
                               visi.com
Reply to
Grant Edwards

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.

Meindert

Reply to
Meindert Sprang

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.

Paul

Reply to
Paul Keinanen

'const' has nothing to do with the issue at hand.

^^^^^^^^^^^^^^^ ^^^^^^^

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.
Reply to
Hans-Bernhard Broeker

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.