0

Let's say I have the following array:

GLfloat vertex[(vertexnm+2)][3];

A simple assignment to an element of the outer array is not possible:

vertex[0] = {0.0f, 0.0f, 0.0f};

Which results in Array type 'GLfloat [3]' is not assignable.

The following way seems to be possible:

*(vertex[0]) = *new GLfloat[3] {0.0f, 0.0f, 0.0f};

But it does not seem like it's a good solution. Is there a clean way to do this?

1
  • You might use std::array. Commented Jun 8, 2013 at 23:50

3 Answers 3

2

Unfortunately arrays are only little more than glorifed pointers in C which e.g. means here that they don't copy like you expect (e.g. like objects).

C++11 introduces a new abstraction for arrays which make arrays behave much more like you expect, e.g. they can be copied or assigned. With pre-C++11 compilers/standard libraries you can use the array class from TR1 in tr1/array.

#include <array>

int main() {
  typedef std::array<GLfloat, 3> vertex;
  std::array<vertex, vertexnm + 2> a;
  a[0] = vertex{0, 0, 0};
}
Sign up to request clarification or add additional context in comments.

Comments

2

Even though C-style arrays are certainly not just "glorified pointers", naked C-style arrays are not assignable, regardless of how you slice it. C++11 does not change anything in that regard.

Your

*(vertex[0]) = *new GLfloat[3] {0.0f, 0.0f, 0.0f};

does not do what you think it does. On the left-hand side vertex[0] decays to the pointer to vertex[0][0], which you dereference with the *. So, the left-hand size is simply vertex[0][0].

Meanwhile, new GLfloat[3] {0.0f, 0.0f, 0.0f} returns a pointer to the [0] element of the newly allocated nameless array. The * dereferences that pointer, giving you access to that [0] element.

The above means that your assignment is really equivalent to

vertex[0][0] = nameless_dynamic_array[0];

i.e. it does

vertex[0][0] = 0.0f;

with the new-ed array becoming a memory leak.

In order to assign an array as a whole, you have to wrap it into a class (std::array being a standard wrapper). Or, if you for some reason have to use naked C-style arrays, use std::copy or even memcpy for copying data from one array to the other.

Comments

0

Here is another alternative:

 static const int myFirstArray[] = {16,2,77};
 static const int mySecondArray[] = {84,64,1};

 std::vector< std::vector<int> > myArray(3);
 myArray[0] = std::vector<int>(myFirstArray, myFirstArray + sizeof(myFirstArray) / sizeof(myFirstArray[0]) );
 myArray[1] = std::vector<int>(mySecondArray, mySecondArray + sizeof(mySecondArray) / sizeof(mySecondArray[0]) );
 //prints:
 //16 2 77
 //84 64 1
 for (unsigned int i=0;i<myArray.size();++i){
    for (unsigned int j=0;j<myArray[i].size();++j){
        std::cout << myArray[i][j] << " ";
    }
    std::cout << std::endl;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.