Cannot set variable length to variable

What I'm trying to do right now is create an array with the length specified by the variable. However, when I put the variable in the length of the array, it gives me a "variable length array of non-POD element type of type" glm :: vec2 ". However, if I replace the variable with an actual number, the error goes away. Why is this happening and how can I do it fix?


int numtriangles = sector1.numtriangles;


glm::vec2 tex[test]; //Using a variable generates an error
glm::vec3 vertices[10]; //No error here


      

+3


source to share


5 answers


You cannot have variable length arrays (VLA) in standard C ++.
Standard variable lengths are not allowed by the C ++ standard. In C ++, the length of an array must be constant at compile time. Some compilers support VLA as a compiler extension, but using them makes your code not portable to other compilers.



You can use std::vector

VLA instead.

+7


source


See this question Is there a way to initialize an array with volatile variables? (C ++)

The short answer is: no, you cannot directly do this. However, you can get the same effect with something like

int arraySize = 10;
int * myArray = new int[arraySize];

      

Now myArray

is a pointer to an array, and you can access it like an array, for example myArray[0]

, etc.

You can also use a vector, which will allow you to have a variable length array. In my example, you can create an array with an initailizer variable, however it myArray

will only contain 10 elements in my example. If you don't know how long the array will ever use the vector and you can click and drop it.



Also note in my example that since you have allocated memory allocated you need to free that memory when you are done with the array by doing something like

delete[] myArray;

      

Here is a small sample app to illustrate the point

#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    int arraySize = 10;
    int * myArray = new int[arraySize];
    myArray[0] = 1;
    cout << myArray[0] << endl;

    delete[] myArray;
}

      

+4


source


use STL. IF you want a variable length array you can use vectors in#include<vector>

Array of array of variable length up to 32 cm long.

+1


source


When you declare an array with a length specifier, only constants are allowed.

Actually, when the program is compiled, the length of the array is calculated.

Note that it is illegal in C ++ to declare int test[];

that the compiler has no way of knowing how much space is allocated for a variable.

Without a length specifier, there is no real memory reserved for the array and you need to use pointers and dynamic memory allocation:

int * test = new int[12];
// or
int * test = new int[val]; // variable works here

// and don't forget to free it
delete [] test;

      

The use int test[12]

actually creates an array that is statically initialized once and for all to hold 12 integers at compile time. Never try to do delete [] test

with a variable declared this way, as it will definitely crash your program.

To be precise, if an array is declared in a function, it will use a space on the program stack, and if declared in a global context, program data memory will be used.

0


source


C ++ does not support variable length array declarations. Therefore, to use an array with length, you can

  • Suppose a large number, which is the maximum possible length of your array. Now let's declare an array of this size. And use it assuming it is an array of your desire length.

    #define MAX_LENGTH 1000000000
    glm::vec2 tex[MAX_LENGTH];
    
          

    to iterate

    for(i=0; i<test; i++) {
        tex[i];
    }
    
          

    Note: Memory usage is not minimized in this method.

  • Use a pointer and highlight it according to your length.

    glm::vec2 *tex;
    tex = new glm::vec2[test];
    
          

    enter code here

    for(i=0; i<test; i++) {
        tex[i];
    }
    delete [] tex; // deallocation
    
          

    Note. freeing memory twice will result in an error.

  • Use a different data structure that behaves like an array.

    vector<glm::vec2> tex;
    for(i=0; i<test; i++){ 
        tex.push_back(input_item);
    }
    /* test.size() return the current length */
    
          

0


source







All Articles