Hi, I think I came across a compiler bug.
The following example throws on intel c++ compiler 2017.4.210
This is using c++14, Windows 7, Visual studio 2017.
#include <vector> #include <stdexcept> struct A { int a; int b; int c; int d; }; struct B { A a; std::vector<int> b; }; int main() { B b{}; if (b.a.a || b.a.b || b.a.c || b.a.d) throw std::runtime_error("Compiler bug?"); }
b is initialized using empty braces, so it should performe aggregate initialization. b.a should be zero-initialized, but it is left uninitialized.
Link Copied
Hi,
Thanks for reporting this bug. Ideally, it should have set the struct variables to 0.
We will inform this to the concerned team, in order to get it fixed.
Regards,
Goutham
I've reported this issue to our Developer.
I have encountered a similar bug in compiler version 19.1 on linux. Aggregate initialization just doesn't initialize in some cases.
aggregate.cpp:
#include <cstdio> //no bug if this is constexpr inline int zero() { return 0; } struct sub { int v1 = zero(); int v2 = 42; }; struct agg { sub s[2]; }; int main() { printf("%d\n", agg{ }.s[0].v2); return 0; }
$ icc -v && icc -O0 aggregate.cpp && ./a.out
icc version 19.1.0.166 (gcc version 8.3.1 compatibility)
1230900048
$ gcc -v && gcc -O0 aggregate.cpp && ./a.out
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
42
This is a pretty serious bug given how common empty brace initialization is. Is there any ETA on when this might be fixed?
As a workaround, you can compile with -Wmissing-field-initializers but you might find a lot of empty braces to fill in.
We don't have any ETA to share as this moment.
For more complete information about compiler optimizations, see our Optimization Notice.