The reason BSS segment cleared

The reason is clear BSS segment because the section is BSS 

Why should BSS say, then, history is relatively old. I know the origin of the BSS segment Unix is ​​the first time (of course, does not exclude the possibility that earlier case). Two types of variables: local variables, global variables.

C in accordance with the syntax, the local variable is not set initial value, the initial value of which is uncertain, the storage position of the local variables (excluding static local variable) is located on the stack, the specific location is not fixed.

Global variables (static and local variables) have a special data segment is stored, the initial value is 0, the specific location is fixed.

In fact, in the final analysis, they are two, one is a fixed position (in the data segment), one is not fixed in position (on the stack). 

You know, the early computer storage devices are very expensive, and very often, the data segment in the global variables are zero (or no initial value), then the storage so many 0 to target file is actually not necessary. Therefore, in order to save space, when the target file is generated, not put the initial value (actual is 0) variables in the data segment are put in the BSS, so that the target file does not need so much in volume (save disk space ). Only when the target file is loaded, the loader is responsible for clearing the BSS segment (one cycle can handle). After this rule is slowly becoming a standard configuration, most compilers will support the BSS segment.

 Then explained a few questions: Q: Why are local variables initial value other than 0? A: The initial value of the local variables can also be zero (in some languages ​​is), but in fact need to consume hardware instructions to complete, sometimes not such a clear sense of movement, the compiler is also a kind burden, each call to the function consumes instruction to clear the burden is too big. To know only one global variable in memory, local variables (non-static) may be more than the former one can be cleared up, the latter repeatedly cleared, the burden is too big. 

Q: Can not cleared if the BSS? A: Yes, if the compiler provisions BSS section is not cleared, is also possible, but then we must change the C language syntax: uninitialized global variables and static local variable, its value is unknown. Even other languages ​​have to follow the change syntax. 

So, the reason is because the BSS segment cleared the BSS segment is now so cheap storage media, is not BSS is no longer necessary? Of course not, cheap PC and digital media products in this, the embedded industry never exist only the storage medium is not limited.

Guess you like

Origin www.cnblogs.com/lvzh/p/12079365.html
BSS