Monday, February 23, 2009

BSS Section in Process Structure & Its Advantage

BSS Section in Process Structure & Its Advantage

---Global variable's life time is until the completion of the program. But what is the point of having two sections for global variables based on initialized and uninitialized. The point is to reduce the executable size of the program before going to discuss about the BSS section we need to understand what process structure, and how the structure looks in the memory (RAM) at the time of execution.




---I didn't say all process structure will be looks like the above, Its based on the operating system but the above section will be present in all operating system' s process structure(minimum section).

---Here we are to discuss about the BSS section. How it reduces the executable size of the program?

---If we are not initialized the global variables the loader will take care to initialize all global variables to zero (0). The loader is designed in such a way to initialize the global to zero while loading the process into RAM. If we are keeping initialized and uninitialized variable in the same block loader will initialize to zero for all the initialized variables too. Compiler developer decided to divide the global sectioninto two parts

1. DATA
2. .BSS

---All initialized variable will go to DATA section, this will be decided in compilation time itself. But uninitialized variable will go into BSS section. This block will be created at the time of loading so it reduces the executable size of the program.

---If you want to confirm this?
open Microsoft Visual C++ 6.0
Run the below two program using Visual C++ 6.0 see the executable file size


Size of this executable 167,969 bytes

------------------------------------------

size of this executable 48,168,993 bytes

--------------------------------------------




For Building the project



For executing the program


Where ever u saved this file there u can see another one directory called "Debug". if you open the directory you can find the executable file. check the size of the file

No comments: