I tested these two examples in Code::Block with gcc compiler on Windows x64:
#include <stdio.h>
int global;
int main(void)
{
static int i=1000;
return 0;
}
I compiled this demo with gcc in command line as:
C:\Demo_CPP\mem01>gcc main.c -o mem5-layout
C:\Demo_CPP\mem01>size mem5-layout.exe
I got this memory output:
text data bss dec hex filename
9708 2216 2440 14364 381c mem5-layout.exe
Now I made small change and intialize this var global=100 as below:
#include <stdio.h>
int global=100;
int main(void)
{
static int i=1000;
return 0;
}
now I compile and output this memory layout from command line:
C:\Demo_CPP\mem01>gcc main.c -o mem6-layout
C:\Demo_CPP\mem01>size mem6-layout.exe
I got this memory output:
C:\Demo_CPP\mem01>size mem6-layout.exe
text data bss dec hex filename
9708 2216 2432 14356 3814 mem6-layout.exe
compare these two memory layout, BSS value changed as expected: because var global is initialized to 100, so it is removed from BSS to DS segment,
But why data segment value(2216) does not change?
What I have tried:
from theory, this data segment value shall increase because int global is moved into data segment.
how to explain this data segment value not changing?