Memory layout in compiled languages
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
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
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
Since it is static int i
is initialized it increased the data segment and decreases bss.
memory_prog05.c
Since it is int global = 10;
is initialized it increased the data segment and decreases bss.