tags:

views:

329

answers:

3

I read in my unix text book that bss is used to store the uninitialized variables(global declarations). This would mean that the static variables are stored separately and not in the bss. The only other place is the data segment.

Here is my question: Is bss segment a part of the data segment or are they two entirely different set of memory segments? Also, why keep the global variables separately from the static variables?

A: 

have a look here: http://en.wikipedia.org/wiki/.bss

Tony
A: 

bss is traditionally the so-called uninitialized variables, but C and Unix guarantee they are initialized to zero. (See this.) It is a portion of the data segment, usually right after the program-statically-initialized-variable space. Just higher in memory than bss is usually the beginning of the heap.

wallyk
+8  A: 

The original idea is still there, although there are layers of obfuscation on top of it for local symbols, small references, shared libraries, and such.

But everything is still based on the original simple model and so it is worth knowing. The idea is that there are three segments.

  • .text
    This has program code.
  • .data
    This has initialized data.
  • .bss
    This has uninitialized data.

Bss is special: .bss objects don't take any space in the object file, and by grouping all the symbols that are not specifically intialized together, they can be easily zeroed out at once.

Static has a couple of meanings. In one sense it is just a symbol attribute that tells the linker whether it is local to an object module or can be globally linked with other symbols of the same name. It is also used to specify storage duration, to distinguish objects within a function from automatic (stack allocated) objects.

So a static variable can be in either data or bss depending on whether it is initialized or not, as can a global.

DigitalRoss
Great answer, but you forgot .rodata :-)
Mads Elvheim
Heh, also, I didn't mentioned the great undefined-with-value trick. "Common blocks" (every bss symbols, including fortran common and individual C extern or global uninitialized objects) could have values, which were their size. The linker took the max value and allocated bss space, if still undefined (and defined it) but if a .data segment label defined the symbol then it won. This simple technique was remarkably effective, handling all known languages, I think. Certainly it handled all the common ones.
DigitalRoss