Reputation: 135
I have been coding for a while but C++ is pretty new to me.
I know that there are static and dynamic arrays in C++. Static arrays are assigned memory during the compile time and dynamic during runtime in heap. And dynamic arrays are declared as:
data-type * variable = new data-type[value];
For eg:-
int*a = new int[n];
And static arrays are declared :
int a[n]; //where n already has value during the compilation time.
So, my question is-
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int a[n][n]; //static array
}
Why this code doesn't run in compilation error since the value of n is defined during runtime. So how can the static array with a variable n be defined during compilation?
Upvotes: 0
Views: 109
Reputation: 85837
ideone.com uses g++ to compile C++ (this is mentioned in their FAQ).
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++.
(Emphasis mine.)
In other words, this is a non-standard language extension supported by gcc.
Upvotes: 1