I am verifying the struct member initialization. The compiler is gcc 4.8.5
and the code is like this:
#include <stdio.h>
typedef struct
{
int m1;
int m2;
} A;
int main(void) {
A a;
A b = {.m1 =1};
A c = {1};
printf("%d, %d\n", a.m1, a.m2);
printf("%d, %d\n", b.m1, b.m2);
printf("%d, %d\n", c.m1, c.m2);
return 0;
}
The result of executing it is:
-1498088800, 32765
1, 0
1, 0
And the assembly code is:
0x0000000000400530 <+0>: push %rbp
0x0000000000400531 <+1>: mov %rsp,%rbp
0x0000000000400534 <+4>: sub $0x30,%rsp
0x0000000000400538 <+8>: movq $0x0,-0x20(%rbp)
0x0000000000400540 <+16>: movl $0x1,-0x20(%rbp)
0x0000000000400547 <+23>: movq $0x0,-0x30(%rbp)
0x000000000040054f <+31>: movl $0x1,-0x30(%rbp)
0x0000000000400556 <+38>: mov -0xc(%rbp),%edx
0x0000000000400559 <+41>: mov -0x10(%rbp),%eax
0x000000000040055c <+44>: mov %eax,%esi
0x000000000040055e <+46>: mov $0x400640,%edi
0x0000000000400563 <+51>: mov $0x0,%eax
0x0000000000400568 <+56>: callq 0x400410 <printf@plt>
From the assembly code:
0x0000000000400538 <+8>: movq $0x0,-0x20(%rbp)
0x0000000000400540 <+16>: movl $0x1,-0x20(%rbp)
0x0000000000400547 <+23>: movq $0x0,-0x30(%rbp)
0x000000000040054f <+31>: movl $0x1,-0x30(%rbp)
I can see if I initialize part members of structure, other members are default set to 0
. Does this comply with C
specification? Or just depends on the compiler?