I compiled below code with cygwin GCC on a x64 machine:
gcc main.c -o main
(main.c)
long long mango = 13; // I also tried `char`, `short`, `int`
long melon = 2001;
void main()
{
}
Then I dump the symbol values with nm
:
./main:0000000100402010 D mango
./main:0000000100402018 D melon
As I understand, a symbol's value just means its address. So the address for mango
is 100402010
. And melon
has address 100402018
. So mango
should occupy 8 bytes.
I tried other types for mango
, such as char
, int
, short
. It is always 8 bytes occupied.
Why the size doesn't change?
ADD 1
Thanks to the comment.
I just tried below code:
typedef struct{
char a1;
char a2;
char a3;
char a4;
char a5;
} MyStruct;
MyStruct MyObj1={1,2,3,4,5};
MyStruct MyObj2={1,2,3,4,5};
long long mango = 13;
long melon = 2001;
void main()
{
}
And this time, nm
shows me this:
./main:0000000100402020 D mango
./main:0000000100402028 D melon
./main:0000000100402010 D MyObj1
./main:0000000100402015 D MyObj2
MyObj1
and MyObj2
are 5 bytes separated. So it is indeed up to the compiler to decide the padding.