Memory layout in compiled languages

memory_prog01.c

operating-systems/memory_prog01.c at main · sanchezcarlosjr/operating-systems
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or window. Reload to refresh your session. Reload to refresh your session.
https://github.com/sanchezcarlosjr/operating-systems/blob/main/memory_prog01.c

It assigns the text segment to the below assembly code, the variables in .section (outside the main) are in the data section, bss memory segment (block starting symbol) is allocated to uninitialized global, local, and external variables, dec is the sum the size bss, data, text, and hex is the same number in hexadecimal.

.text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    $0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (GNU) 12.1.0"
        .section        .note.GNU-stack,"",@progbits

memory_prog02.c

operating-systems/memory_prog02.c at main · sanchezcarlosjr/operating-systems
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or window. Reload to refresh your session. Reload to refresh your session.
https://github.com/sanchezcarlosjr/operating-systems/blob/main/memory_prog02.c

The compiler removed dead code (it removes int global but it should be in bss segment), so the operating system allocated the same memory layout previous program.

memory_prog03.c

operating-systems/memory_prog03.c at main · sanchezcarlosjr/operating-systems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters You can't perform that action at this time. You signed in with another tab or window.
https://github.com/sanchezcarlosjr/operating-systems/blob/main/memory_prog03.c

The compiler removed dead code (it removes int global but it should be in bss segment), but increase by static int i (i is in bss segment)so the operating system allocated it and increase bss segment.

memory_prog04.c

operating-systems/memory_prog04.c at main · sanchezcarlosjr/operating-systems
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or window. Reload to refresh your session. Reload to refresh your session.
https://github.com/sanchezcarlosjr/operating-systems/blob/main/memory_prog04.c

Since it is static int i is initialized it increased the data segment and decreases bss.

memory_prog05.c

operating-systems/memory_prog05.c at main · sanchezcarlosjr/operating-systems
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or window. Reload to refresh your session. Reload to refresh your session.
https://github.com/sanchezcarlosjr/operating-systems/blob/main/memory_prog05.c

Since it is int global = 10; is initialized it increased the data segment and decreases bss.