1

The following code works fine on my C++ compiler despite the fact that it initializes a variable-sized object.

#include <iostream>

int main() {
    int n; std::cin>>n;
    int numbers[n]={1,2,3};
}

I'm using g++ 11.2.0 on Ubuntu 21.10. My IDE issues the warning Variable-sized object may not be initialized, but the code still seems to work fine. Why?

  • GCC supports it as a non-standard language extension. Use the `-pedantic-errors` flag to make g++ refuse to compiler code using such extensions. – user17732522 Mar 01 '22 at 08:12
  • @πάντα The linked duplicate doesn't really explain why it _does_ work with g++. OP seems to be aware that VLA is not standard C++. – user17732522 Mar 01 '22 at 08:16
  • So try this as a duplicate instead [Variable Length Array (VLA) in C++ compilers](https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers) – BoP Mar 01 '22 at 08:52

0 Answers0