Jan 13, 2011 at 11:15am UTC
If you had read the chapter on arrays in c++ - you would have been aware that array subscripts
have to be a constant value - For example:
1 2 3
const int size = 50;
int arr[23];
long numRows[size];
Last edited on Jan 13, 2011 at 11:27am UTC
Jan 13, 2011 at 2:24pm UTC
In C++, you don't need to do that
1 2 3 4 5 6 7 8 9 10
int main()
{
long array[];
GetSize(&size);
long * array = new long [size];
if (array != NULL) delete array;
}
just use vector instead of handcrafting array
besides, you cannot write something like
you must tell the compiler how many space do you need before the run time
instead, you should
1 2 3 4 5 6 7 8 9 10 11 12 13 14
GetSize(&size);
int *array = 0;
try
{
array = new int [size];
}
catch (std::bad_alloc &memAllExcept)
{
std::cerr<<memAllExcept.what()<<"\n" ;
}
delete []array;
use try & catch to handle the possible errors of memory allocation
puts the array on the Heap instead
vector would put the "array" on the heap, same like the handcrafting code
but it is safer and easier to use than handcrafting array like above
Last edited on Jan 13, 2011 at 2:30pm UTC