Harsh Jain
Harsh Jain

Reputation: 135

Why static array with length not initially defined don't result in compilation error in C++?

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

Answers (1)

melpomene
melpomene

Reputation: 85837

ideone.com uses g++ to compile C++ (this is mentioned in their FAQ).

The gcc documentation says:

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

Related Questions