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
Note that
-ftrivial-auto-var-init
only applies to automatic variables (like the name suggests):Automatic variables are variables that have automatic storage duration, i.e. local block-scope variables only.
Note that if you're using
new
/delete
then your objects will have dynamic storage duration and therefore-ftrivial-auto-var-init
will not apply to them.Example
This example demonstrates the effect of
-ftrivial-auto-var-init=zero
:godbolt
When compiled with
-ftrivial-auto-var-init=zero
you're guaranteed to get:Whereas if you compile without it you'll most likely get random bytes from the stack (in this case gcc chose to reuse the space of
buf
):