Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the BSS segment store?

Tags:

loader

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 initialized and my second question is I read that BSS segment doesn't consume memory, then where those it store these variables? Thanks

like image 900
hue Avatar asked Apr 25 '11 06:04

hue


People also ask

What is stored in data segment?

A data segment is a portion of the virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. Note that, the data segment is not read-only, since the values of the variables can be altered at run time.

What variables are stored in BSS?

All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

What is BSS segment used for?

bss section is used by the compiler for global and static variables. It is one of the default COFF sections that is used to reserve a specified amount of space in the memory map that can later be used for storing data. It is normally uninitialized.

What is stored in code segment?

In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions.


2 Answers

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.

like image 52
Greg Hewgill Avatar answered Sep 20 '22 02:09

Greg Hewgill


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.

like image 32
Jeff Mercado Avatar answered Sep 17 '22 02:09

Jeff Mercado