my first own question at Stack Overflow:
I am working on a C++ program where something happens that I thought would not work. My colleague is setting up a vector myvec
of elements of some class we are working with. Its size is chosen by the user on calling the program, so it is not fixed when compiling! Nevertheless, the code includes the following statement which is working:
const unsigned myNumber = myvec.size();
bool valid_PDFplot[myNumber];
I always thought that you cannot define an array of a size that is not already known at compile time unless you are using dynamically allocated memory with new
? As said above, the size of the vector is not set at compile time as the user has the possibility to add an arbitrary number of elements using push_back
. Why is the code shown above still working?