std::array of pointers to structs and c++11 for loops

Hello all,

I created an array of pointers to structs using std::array

and I get the beloved segfault...

Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::array<tri*, 128> triangles;

triangles[0] = new tri {
  1.0f, 1.0f, 1.0f,
  1.0f, 1.0f, 1.0f,
  1.0f, 1.0f, 1.0f
};

...

for (tri* i : triangles) {

  // Only pass if the variable exists
  if (i) {

    std::cout << i->aX; // << it segfaults here

  }

}


Thanks for any help

EDIT:

std::cout << triangles.size() << std::endl; returns 128 even before the initialization of the first element *scratches head*
Last edited on
FIXED!

Solution: fill the array with NULL pointers before declaring any elements
Topic archived. No new replies allowed.