https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77992
Lei YU changed:
What|Removed |Added
CC||mine260309 at gmail dot com
--- Comment #18 from Lei YU ---
I hit the exact same problem with GCC 7.4.0 and 9.2.1, and finds out that:
* When all fields are initialized, the padding bytes are not initialized;
* When there is a field is not provided with init value, the padding bytes are
initialized to 0.
See below code snippet:
#include
#include
#include
struct struct_with_padding {
uint32_t a;
uint64_t b;
uint32_t c;
};
int main()
{
struct struct_with_padding s;
memset(&s, 0xff, sizeof(s));
s = (struct struct_with_padding) {
.a = 0x,
.b = 0x,
#ifdef SHOW_GCC_BUG
.c = 0x,
#else
//.c = 0x,
#endif
};
uint8_t* p8 = (uint8_t*)(&s);
printf("data: ");
for (size_t i = 0; i < sizeof(s); ++i)
{
printf("0x%02x ", p8[i]);
}
printf("\n");
return 0;
}
With `.c = 0x`, the example output:
data: 0xaa 0xaa 0xaa 0xaa 0xff 0xff 0xff 0xff 0xbb 0xbb 0xbb 0xbb 0xbb 0xbb
0xbb 0xbb 0xdd 0xdd 0xdd 0xdd 0xff 0xff 0xff 0xff
Without that, the example output:
data: 0xaa 0xaa 0xaa 0xaa 0x00 0x00 0x00 0x00 0xbb 0xbb 0xbb 0xbb 0xbb 0xbb
0xbb 0xbb 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
So it looks like GCC could be improved by initializing the padding bytes in
both cases?