How are global variables stored?

AFAIK, there are 2 types of global variables, initialized and unintellized. How are they stored? Are they saved in an executable file? I can think of initialized global variables having their initial values ​​stored in an executable. But what needs to be stored for uninitialized ones?

My current understanding is this:

The executable file is organized into several sections, such as .text, .data and .bss. Code is stored in the .text section, initialized global or static data is stored in the .data section, and uninitialized global or static data is stored in the .bss section.

Thanks for watching my questions.

Update 1 - 9:56 AM 03/11/2010

I found a good link here:

Segments in the source of the assembly language - Build segments of text and data using the directives .text, .data and .bss

Update 2 - 10:09 AM 03/11/2010

@Michael

  • I define 100 bytes of an uninitialized data area in my assembler code, this 100-byte is not stored in my executable file because it is NOT initialized.

  • Who will allocate 100-byte uninitialized memory space in RAM? Program loader?

Suppose I got the following code:

int global[100]; void main(void) { //... } 

Global [100] is not initialized. How will global [100] be transcoded in my executable? And who will single it out at what time? What if it is initialized?

+6
c ++ c assembly
source share
4 answers

The initialized variable values ​​are stored in the .data segment of the executable file. Uninitialized ones do not need to be stored. They end in the .bss segment in RAM, but the segment size is zero in the executable file, only the necessary amount of memory is stored in the segment descriptor. The code in the .text section accesses them through offsets in the segment. The runtime linker-loader corrects these links to actual virtual addresses . See, for example, The executable and related format that is used on most Unix-like operating systems.

+7
source share

The storage of global variables is allocated in your virtual computer memory by the linker / bootloader during the loading of your program. The actual storage of global variables is located somewhere in the hierarchy of physical memory (cache, RAM, SSD / HD backup storage, etc.), which are displayed in the cache and the VM system. Everything could become quite fragmented.

The initialized global values ​​are copied from the .data segment to a portion of the allocated virtual memory. Uninitialized global values ​​may be reset or lost in them, depending on the security of the particular OS in which the program runs.

Other options, depending on the language, compiler, runtime language and OS.

+2
source share

In the PE files for each segment, two sizes are indicated: RAWsize (size on disk) and Vsize (size in RAM).

When Vsize larger than RAWsize , the rest of the segment in RAM is nullified.

.bss (if present) always has RAWsize 0, and there are uninitialized globular variables.

Another common approach is to make Vsize of .data larger than its RAWsize , so that the rest of the segment will contain unified variables.

+1
source share

Uninitialized variables are just machine level pointers. Space is allocated for them at runtime, and the program will fill it after a while.

For example, if in assembler you create a global variable global BYTE 100 , which will reserve global as a pointer to a 100-byte region. The program then has access to this region for what it needs.

EDIT: I looked in my build, and it looks like uninitialized globals are defined in the .data section in the same way as initialized variables. In my opinion, space is allocated in exe (say 100 bytes, as mentioned above), but will contain undefined. On Intel computers on Windows, this will be garbage; The program is responsible for its initialization. Hope this helps!

0
source share

All Articles