I have been teaching myself programming for couple of years, and I was sure that if you need array declaration of a variable number you need to use malloc
or new
.
Today I found that this compiles under g++ version 4.4.4, without warnings or errors:
#include <iostream>
using namespace std;
int main()
{
int size_array;
cin >> size_array;
int iTable[size_array];
for(int i=0;i < size_array;i++)
iTable[i]=i*i;
for(int i=0;i < size_array;i++)
cout << iTable[i] << endl;
return 0;
}
Also it compiles completely fine if you are using gcc (after changing cout
and cin
with printf
and scanf
)
Under Visual Studio this code fails to compile since size_array
is not constant.
When this was changed? This is a safe method?
Arrays are Not Constants It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
because it immediately violates the rules of const-correctness. so what you are saying is that there is no implicit conversion taking place on x "from int to const int " during reference binding is that right? If yes can you provide link to the standard of the statement which proves your point..
You can't. The const keyword is used to create a read only variable. Once initialised, the value of the variable cannot be changed but can be used just like any other variable. That means, a const qualified variable must not be assigned to in any way during the run of a program.
This is a C99 feature - VLA - which is not a part of standard c++. You can use it if your compiler supports it and you don't require portability. If the compiler supports it, it's perfectly safe to use - but it's a bad habit using non-standard features.
This is a compiler extension of gcc, not standard.
No thats not safe at all. It could corrupt your stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With