Can't set variable length with variable

前端 未结 5 1273
梦如初夏
梦如初夏 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

      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

      Note: deallocation of memory twice will occur a error.

    3. Use other data structure which behave like array.

      vector tex;
      for(i=0; i

提交回复
热议问题