Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2019 does not handle aggregate initialization of dynamic array of structs correctly

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).

like image 629
Ivan Ivanov Avatar asked Sep 11 '25 00:09

Ivan Ivanov


1 Answers

I got it to work by writing the following but then the data is not stored on the Heap.

Item items[] {
    { 1, 1 },
    { 2, 2 }
};

If you need it on the heap use the solution below it seems to work with the vc++ compiler. (Note that this is only a workaround and does nt fix the underlying problem):

Item* Items[2];
Items[0] = new Item{3,3};
Items[1] = new Item{4,4};

std::cout << (*Items[0]).id << (*Items[0]).type << std::endl;
std::cout << (*Items[1]).id << (*Items[1]).type << std::endl;

Altenatively you can create the Array using the first option and then copy it into an Array on the heap like this:

Item items[2]{
    {1,1},
    {2,2}
};

Item* hitem = new Item[2];
for(int i = 0; i < 2; i++){
    hitem[i].id = items[i].id + 4;
    hitem[i].type = items[i].type + 4;
}

While this is slow it works like it is supposed to even on the vc++ compiler. You can view the whole code here:https://rextester.com/VNJM26393

I don't know why it only works like this...

like image 69
Noa Sakurajin Avatar answered Sep 12 '25 14:09

Noa Sakurajin