Search code examples
c++visual-studiofunctor

Modifying array size at runtime


I would like to know if there is a possibility of declaring the array size at runtime without using the new keyword.

struct myKernel
{
        uint8_t **var;
        myKernel(){}
        myKernel(const int number = 2)
        {
//I want to do something like
//var = new int*[number]; but not allocate it on the heap
           *var = a[number];
        }
        void operator()   
       {
        //do stuff here with the var array

       }
           
};

Solution

  • If you want something to be allocated at runtime (i.e based on runtime information) then you have to use heap-based allocation.

    An alternative for your need could be to use a std::vector and to use resize but there would still be some dynamic allocation somewhere.