0

Ok, so I have a model class that contains a pointer to (what will be) an array of point3 objects:

point3* _vertices_colors;

Point3 has the following typedef:

typedef GLfloat point3[3];

Essentially making an array of point3 objects an array of arrays. Then in a derived classes' constructor, I allocate memory for the number of vertices and colors I want to store as follows:

_vertices_colors = new point3[16];

This means my object has 8 vertices with their own colors stored. I then define the following array on stack ready to copy to the pointer:

point3 verticesColors[] = {
                            {1.0, 1.0, 1.0}, {1.0, 0.0, 0.0},
                            {-1.0, 1.0, 1.0}, {1.0, 0.0, 0.0},
                            {-1.0, -1.0, 1.0},{1.0, 0.0, 0.0},
                            {1.0, -1.0, 1.0},{1.0, 0.0, 0.0},
                            {1.0, 1.0, -1.0}, {1.0, 0.0, 0.0},
                            {-1.0, 1.0, -1.0}, {1.0, 0.0, 0.0},
                            {-1.0, -1.0, -1.0},{1.0, 0.0, 0.0},
                            {1.0, -1.0, -1.0},{1.0, 0.0, 0.0}
                        };

Then, I use a for loop to copy to the array on heap:

for(int i = 0; i < 16; i++)
{
    *_vertices_colors[i,0] = *verticesColors[i, 0];
    *_vertices_colors[i,1] = *verticesColors[i, 1];
    *_vertices_colors[i,2] = *verticesColors[i, 2];

    printf("%15f", *_vertices_colors[i,0]);
    printf("\t");
    printf("%15f", *_vertices_colors[i,1]);
    printf("\t");
    printf("%15f", *_vertices_colors[i,2]);
    printf("\n");
}

However, this appears to assign 1.0, 1.0, -1.0 to each of the 16 rows of the array. I've tried other ways of assigning the pointer to the array, for example the line:

_vertices_colors = verticesColors;

As verticesColors is a constant pointer to an array, I thought this would work, however it produces the same results. I also tried using memcpy:

memcpy(_vertices_colors, verticesColors, sizeof(_vertices_colors));

But this seems to produce some uncontrollable results. It assigns each of the first columns as 1.0 and the rest as very large negative integers. Can anyone see why my first method doesn't work?

fanatic
  • 143
  • 2
  • 9
  • 1
    [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). I suggest using `typedef std::array, N> M> MyArray; MyArray a; MyArray *b = new MyArray(a);` or similar, C++, styles – sehe Nov 02 '12 at 12:50
  • @H2CO3 No, but an array's identifier is a constant pointer to the first element. That's what I meant to say. – fanatic Nov 02 '12 at 13:38

1 Answers1

5

This

*_vertices_colors[i,0] = *verticesColors[i, 0];
*_vertices_colors[i,1] = *verticesColors[i, 1];
*_vertices_colors[i,2] = *verticesColors[i, 2];

is equivalent to

*_vertices_colors[0] = *verticesColors[0];
*_vertices_colors[1] = *verticesColors[1];
*_vertices_colors[2] = *verticesColors[2];

You use a sequence operator , in the array subscription, which yields the last value of the sequence. In this case 0, 1 and 2.

Multi dimensional arrays are accessed as

_vertices_colors[i][0] = verticesColors[i][0];
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • If you explained what the comma did, this would be a good start of an answer – sehe Nov 02 '12 at 12:52
  • This does work, just something left over from moving from C# to C++ I suppose. In C# a ',' is used for multidimensional arrays whereas a '[i][0]' would be to access a jagged array. – fanatic Nov 02 '12 at 13:41
  • Bit late to notice you added the explanation. Anyways, +1 – sehe Nov 08 '12 at 00:07