I thought that I understood what a VLA is, until I saw a question asked here about the difference between dynamic memory allocation vs variable length arrays. So I don't have any problem, at least for now, with dynamic memory allocations, but what I don't understand is why this code is considered a VLA:
int size = 10; // or whatever
int my_array [size] ; // why this is a VLA
The more mysterious thing is that this is even supposed to be a VLA
const int size = 10;
int my_array [size]; // why even when i use a const it is a VLA .
So, my question is, how are these 2 different methods considered as VLA?
As far as I know, a VLA is just an array that its size is only know at runtime, like if I prompt the user to enter the size of the array and set the array to this size, so that there is no way the compiler will ever know the size of the array.
But, in both pieces of code above, the size
is already known at compile time, so if that is true that these are VLA, are standard array syntax must be like that and nothing else.
int my_array[10];
My other question is, that I heard that const int size =10;
is not actually a const, which means the compiler doesn't know if this size is 10, it treats it as a variable.
So, if anyone can clear things up a little bit about the difference between a variable and const in C++, that would be really appreciated.
NOTE: this is the link to the StackOverflow question. So, if there is something wrong in this question, someone can correct thing up.
What's the difference between a VLA and dynamic memory allocation via malloc?