I'm getting unexpected output from size
command.
Afaik initialized global and static variables stored in data
segment and uninitialized and initialized to 0 global/static variables stored in bss
segment.
printf("%d",sizeof(int));
gives int
size 4. However, bss
and data
segment is not increasing accordingly to 4.
#include <stdio.h>
int main()
{
return 0;
}
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe
text data bss dec hex filename
10044 2292 2512 14848 3a00 memory-layout.exe
#include <stdio.h>
int g; //uninitialised global variable so, stored in bss segment
int main()
{
return 0;
}
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe
text data bss dec hex filename
10044 2292 2528 14864 3a10 memory-layout.exe
why bss
increased by 16 (2528 - 2512) instead of 4? (in above code)
#include <stdio.h>
int g=0; //initialised to 0 so, stored in bss segment
int main()
{
return 0;
}
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe
text data bss dec hex filename
10044 2292 2512 14848 3a00 memory-layout.exe
there is no increment in bss
in spite of using global variable. why's that?
#include <stdio.h>
int main()
{ static int g; //should be on bss segment
return 0;
}
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.ex
text data bss dec hex filename
10044 2292 2512 14848 3a00 memory-layout.exe
no increment in bss
segment in spite of using static variable, why?
and I have one more question, what dec
represents here?
od
ornm
commands to list object/executable files, their sections and their sections. – Walkoutint g = 0;
- as initialized global is not stored in BSS. – Gallantrydec = text + data + bss
. And` hex` is same asdec
but ...in hex. ;) – Tynanint a; int b; int c; int d; int e;
in stages and noting when it increases in size. Theint g = 0;
case seems to suggest it is going into.data
and didn't increase because there was already room as we just saw. – Kibitzint g = 0;
goes in DATA and some where it goes in BSS. Of course the sensible thing would be for it to go in BSS , IDK whether the compiler vendor just couldn't be bothered or what. In one of my code bases I ended up having to writeint g ZERO_INITIALIZED;
where that is a macro that expands to either blank or to= { 0 }
depending on which compiler/platform is in use – Kibitz