I have an integer num
that was read from a file. I want to create an array with the number of elements being num
.
A sample code of what I want to do but doesn't work:
int num;
cin >> num;
int iarray[num];
I have an integer num
that was read from a file. I want to create an array with the number of elements being num
.
A sample code of what I want to do but doesn't work:
int num;
cin >> num;
int iarray[num];
Arrays in C++ have compile-time bounds.
Use dynamic allocation instead, or a healthy std::vector
wrapper around the same process.
dynamic allocation being int * iarray = new int[num];
Just make sure to call delete[] iarray;
at some point to free the memory.