Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
7644 Discussions

Aggregate initialization bug with nested struct

User87135761
Beginner
958 Views

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.

0 Kudos
5 Replies
GouthamK_Intel
Moderator
959 Views

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

Viet_H_Intel
Moderator
959 Views

I've reported this issue to our Developer.

Sawin__Matthew
959 Views

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.

Viet_H_Intel
Moderator
959 Views

We don't have any ETA to share as this moment.

Viet_H_Intel
Moderator
471 Views

This issue is fixed in oneAPI2021.2.

$ icpc -V

Intel(R) C++ Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.2.0 Build 20210228_000000


I am going to close this thread.


Reply