2

I was surprised to see that below code works in C++.

int n;
cin >> n;
int arr[n];

which is violating most of the information available on internet in this context, that, array size should be compile time known. Is the above feature/behavior is universally rolled out and accepted, or it is just compiler dependent or C++ language version dependent?

3
  • it's VLA (variable-length array), a commonly-supported extension, since it is a part of the C standard, but not C++. Commented Jul 2, 2021 at 10:42
  • It is compiler dependent, but pretty common. GCC and Clang for example accept to create VLA by default, but not in their -pedantic mode. Commented Jul 2, 2021 at 10:50
  • fwiw, there was a proposal to have something similar to VLAs in C++ (i think it was C++14), but it wasn't accepted eventually Commented Jul 2, 2021 at 11:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.