I know that the BSS segment stores the uninitialized global and static variables and initializes them to zero. But what if the global/static variable is initializ开发者_运维知识库ed and my second question is I read that BSS segment doesn't consume memory, then where those it store these variables? Thanks
You probably read that the BSS segment doesn't consume space in the executable file on disk. When the executable loaded, the BSS segment certainly does consume space in memory. Space is allocated and initialised to zero by the OS loader.
If initialized, global/static variables are stored in the .DATA segment. When you declare data in the .DATA segment, you provide the values to that data so it would have to be stored as part of the executable.
On the other hand, you only declare how much data you need for the .BSS since you don't need to know what the values are. So if your program declared 2 GB of uninitialized memory, that 2 GB does not contribute to the size of your executable, you won't see it until after it is loaded.
精彩评论