3

Given

gcc -c main.C
gcc -lstdc++ -o main main.o

And main.C being

#include <iostream>

int main() { 
    int somany; 
    std::cin >> somany; 
    double ex[somany]; 

    for(int i=0;i<somany;i++){ 
            ex[i]=0.03; 
            std::cout << ex[i]; 
    } 
}

Why does this not result in a compiler error? I thought C++ does not have VLAs?

Executing the program works just fine.

Niall
  • 30,036
  • 10
  • 99
  • 142
ben
  • 5,671
  • 4
  • 27
  • 55
  • 6
    It's a gcc extension. You're right that it's not in the standard. – jrok Sep 16 '14 at 12:41
  • 3
    compile with [--pedantic](http://stackoverflow.com/questions/2855121/what-is-the-purpose-of-using-pedantic-in-gcc-g-compiler) to ensure standard c++ – BeyelerStudios Sep 16 '14 at 12:42
  • How can I compile against the c++ standard only? Is there some flag for gcc? (g++ is not available for me atm) – ben Sep 16 '14 at 12:42
  • @BeyelerStudios one second faster than me, thank you ;) – ben Sep 16 '14 at 12:43
  • If anybody knows how to make the title of the question more specific feel free to edit. – ben Sep 16 '14 at 12:44
  • This may be helpful https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html – Dawid Sep 16 '14 at 12:48
  • I'm surprised this compiles at all, despite linking against the C++ standard library, since you use .C and gcc. Or does .c vs .C take care of that? Yuck! Why not simply follow convention? – Lightness Races in Orbit Sep 16 '14 at 12:54
  • .C extension tells gcc that it is c++ code. At compilation you do not have to specify anything regarding the c++ std. It's only the linking where that is necessary AFAIK. – ben Sep 16 '14 at 13:10

1 Answers1

0

This is a GCC extension and doesn't have anything to do with your approach of compiling with gcc then manually linking the C++ standard library.

The --pedantic compilation flag will typically disable such extensions.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055