The only way to allocate a dynamic array in C ++ without using a heap is to do it on the stack.
void func(int size) { int my_array[size]; }
Then you can pass this pointer to your class:
class stuff { public: stuff(int *ary) : f_array(ary) {} private: int *f_array; }; stuff my_stuff(my_array);
Now, to be honest, I would use std :: vector <> (), as presented in other answers that will allocate memory from the heap, but has all the necessary security measures that will help you write much more secure code.
source share