Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

c - Regarding the bss segment and data segment in Unix

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...