I'm following Does C++ standard guarantee the initialization of padding bytes to zero for non-static aggregate objects? and trying to force GCC and Clang to initialize padding with zero. The -ftrivial-auto-var-init=choice
flag seems to do the trick, but I don't see any difference between using it or not
The original code is here (without using -ftrivial-auto-var-init=zero
), and produces non-zeroed members:
GCC
Foo():
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
Foo{}:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Clang
Foo():
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Foo{}:
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
I've added the -ftrivial-auto-var-init=zero
flag to both GCC 12 and Clang 15. but the results are exactly the same, so I'm a bit confused as to what -ftrivial-auto-var-init=zero
should do.
GCC
Foo():
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0
Foo{}:
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Clang
Foo():
x:[----][----][----][----],v: 0
y:[----][----][----][----],v: 0
z:[----][----][----][----],v: 0
Foo{}:
x:[----][0x42][0x43][0x44],v: 0
y:[----][----][----][----],v: 0
z:[----][0x4A][0x4B][0x4C],v: 0