Jim Hudon wrote:
i need to create an array of a size determined by a non-const variable:
int char sampleArray[ arraySize ];
why does the following not work, and what can i do:
const int constArraySize = arraySize;
int char sampleArray[ constArraySize;
for both of the above, i get the same error message when i compile:
"storage size of 'sampleArray' is not constant"
thanks...
C++ requires that the size of a statically allocated array be known at
compile time.
Your options are:
a) use a dynamically allocated array:
char* sampleArray ( new char [ arraySize ] );
The drawback is that you have to take of memory management yourself.
Therefore, it is probably better to:
b) use std::vector.
#include <vector>
...
std::vector< char > sampleArray ( arraySize );
This will allocate a vector of length arraySize. You can use it almsost as
you would use an array. Upon destruction, memory will be released to the
system automatically.
Best
Kai-Uwe Bux