The code below prints garbage (or zeroes) if compiled with VC++ 2017 and "1122" if compiled with GCC or Clang (https://rextester.com/JEV81255). Is it bug of VC++ or I'm missing something here?
#include <iostream>
struct Item {
int id;
int type;
};
int main()
{
auto items = new Item[2]
{
{ 1, 1 },
{ 2, 2 }
};
std::cout << items[0].id << items[0].type;
std::cout << items[1].id << items[1].type;
}
At the same time it works if elements are of a primitive type (like int
).
{}
is completely ignored so you can write some nonsense likeauto items = new Item[2] { { std::cout, " sdf" , 0.3f} };
. I've tried to find related issues at VS feedback hub (at least the one I've created) but the search there is broken as well... – Subtorridwarning C6001: Using uninitialized memory 'items[0].id'
. Butclang-cl
inside VS2019 works fine. – Maccaronestd::vector
but notstd::array
. The pre-compiler complains of "too many initializer values" and the actual compiler throws aC2440
error with the note: "Invalid aggregate initialization." The above code as-is only warns about accessing unintialized memory. – Scamp