int main()
{
        int i=10;
        char arr[i];
}

I was expecting an error but it is compiling without any issues. I'm assuming the g++ compiler is doing something but not sure how to disable that. This is my compiler info

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)

Edit: I think i should mention that the error I was expecting was of using a non-const variable to declare the array size.

Edited 6 Years Ago by Agni: n/a

Thanks Dave, I tried -pedantic option and it is still only giving a warning to me. Shouldn't it be a nice and proper error? It is quite misleading. Can I ask which compiler and version you used?

> g++ -pedantic arrayTest.cpp
arrayTest.cpp: In function ‘int main()’:
arrayTest.cpp:6: warning: ISO C++ forbids variable length array ‘arr’

Version info:

C:\Users\Dave\Projects\scratch>gcc --version
gcc (GCC) 3.4.5 (mingw-vista special)

Edited 6 Years Ago by Dave Sinkula: n/a

Thanks Dave, I tried -pedantic option and it is still only giving a warning to me. Shouldn't it be a nice and proper error?

Maybe try -pedantic-errors

You need to specify the standard you are compiling against. The GCC defaults to using its own extensions. g++ -Wall -ansi -pedantic ... Hope this helps.

Maybe try -pedantic-errors

You need to specify the standard you are compiling against. The GCC defaults to using its own extensions. g++ -Wall -ansi -pedantic ... Hope this helps.

Thanks guys, -pedantic-errors option worked perfectly and gave an error on compilation. On using -Wall -ansi -pedantic i still get only a warning. So I guess I would have to configure my compiler to use -pedantic-errors.

It would be hard to believe but without this option it compiles even if 'i', in the above code, was uninitialized !!! I wonder what is the size of the array it is allocating in that case?

This question has already been answered. Start a new discussion instead.