I know that arrays have to be given a size when using them, so I tried to, but this piece of code doesn't seem to want to be accepted by the compiler.

data is a std::vector<char> so i tried using the .size() function to use the size of the vector to initialize the array. Maybe there is a different way to go about this, but i want to take each char in the array and sore it into the array.

int fileTempSize = data.size();
char fileTemp[fileTempSize];
6 Years
Discussion Span
Last Post by MasterGberry

C++ 98 does not accept variable sized arrays. You must define the size of an array at compile time. If you cannot know the size at compile time, you will have to allocate memory yourself, or alternatively use one of the C++ STL container classes.


The size of an array must be a constant known at compile time. You should dynamically allocate it char * fileTemp = new char[fileTempsize]; instead. Some compilers are able to do what you tried to do, but it's an extension and is non-standard.


May have to dynamically allocate the memory using "new" and "delete".

Also, in this line:

int fileTempSize = data.size();

int doesn't have the range of the unsigned int, make sure your fileTempSize is the same type as the type returned by data.size()

this can cause problems if the size is greater than std::numeric_limits<int>::max(), it will overflow the variable.

std::vector<char>::size_type fileTempSize = data.size();

Or if you have a compiler that supports the new "auto" keyword..

auto fileTempSize = data.size();

Edited by pseudorandom21: n/a


Meh i feel so dumb, was having a blank spot there, forgot i can allocate the memory xD Ty.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.