Can't set variable length with variable

前端 未结 5 1224
梦如初夏
梦如初夏 2021-01-14 14:40

What I\'m trying to do right now is to create an array with a length that is defined by a variable. However, when I put the variable in the array length, it gives me a \"Va

相关标签:
5条回答
  • 2021-01-14 14:45

    C++ doesn't support declare variable length array. So to use a array with a length you may

    1. Assume a big number which is highest possible length of your array. Now declare an array of that size. And use it by assuming that it an array of your desire length.

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

      to iterate it

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

      Note: memory use will not minimized in this method.

    2. Use pointer and allocate it according 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: deallocation of memory twice will occur a error.

    3. Use other data structure which behave like array.

      vector<glm::vec2> tex;
      for(i=0; i<test; i++){ 
          tex.push_back(input_item);
      }
      /* test.size() return the current length */
      
    0 讨论(0)
  • 2021-01-14 14:52

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

    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 the array and you can access it like an array like myArray[0], etc.

    You can also use a vector which will allow you to have a variable length array. My example allows you to create an array with a variable initailizer however myArray will be only 10 items long in my example. If you aren't sure how long the array will ever be use a vector and you can push and pop items off it.

    Also keep in mind with my example that since you've dyanmically allocated memory you will need to free that memory when you are done with the array by doing something like

    delete[] myArray;
    

    Here is a little 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;
    }
    
    0 讨论(0)
  • 2021-01-14 14:54

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

    Native c++ array donot nave variable length array.

    0 讨论(0)
  • 2021-01-14 15:09

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

    You can use, std::vector instead of an VLA.

    0 讨论(0)
  • 2021-01-14 15:09

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

    Actually it's when the program is compiled that the array length is evaluated.

    Note however that it's illegal in C++ to declare int test[]; like the compiler has no way to know how much space to allocate for the variable.

    Without a length specifier, there is no actual memory that is reserved for the array, and you have to resort to using 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;
    

    Using int test[12] actually creates an array that is statically initialized once and for all to contain 12 integers at compile time. Do not ever attempt to do delete [] test with a variable declared this way, as it's most certainly going to make your program crash.

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

    0 讨论(0)
提交回复
热议问题