views:

267

answers:

4

I tried compiling the following code in Linux and VS 2008:

#include <iostream> //  this line has a ".h" string attached to the iostream string in the linux version of the code
using namespace std; // this line is commented in the linux version of the code
void main()
{

  int a=100;
  char arr[a];

  arr[0]='a';
  cout<<"array is:"<<arr[0];

}

This line works in the g++ version but does not work in the Visual Studio. It throws the following error:

1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2057: expected constant expression
1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2466: cannot allocate an array of constant size 0
1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2133: 'arr' : unknown size

Is this a valid statement ?? How can two compilers have different interpretation of the same langauge

+13  A: 

This is a C99 feature:

char arr[a]; // VLA: Variable Length Arrays (C99) but not C++!

GCC supports many features from C99, but VC doesn't and I think it won't in the near future because they are concentrating on C++ more and more. Anyway, you could just change the declaration to:

  const int a=100; // OK the size is const now!
  char arr[a];
AraK
You mean they're concentrating on C#?
Marcin Gil
No, C++. The compiler is usually known as Visual C++. It happens that C++ was forked from c89, so it's easy for C++ compilers to offer a C89 mode for legacy C code. But supporting C99 is a lot more work.
MSalters
Also, C99 features like VLAs are not part of Standard C++, or even of C++0x, so MS are right not to support tem, IMHO.
anon
Note that MSVC's `_alloca` is comparable to glibc's `alloca`, so you can get a valid multi-platform replacement with a little bit of macro-magic. Not sure what's available on other major OSes/architectures.
Tom
A: 

in microsoft c++, it's not valid to create array whose size can't be determined at compile time on stack. You either have to create the array on heap or use a constant to specify the array size.

char *arr = new char[size];
const int size2 = 100;
char arr2[size2];
Raymond
I don't think this has to do with Microsoft. In standard C++, this is not correct. See @Neil answer for more detail why GCC allows that.
AraK
+3  A: 

Try changing int a to const int a. The C++ standard states that the size of arrays should be a constant expression. The definition of a constant expression is (5.19.1):

In several places, C + + requires expressions that evaluate to an integral or enumeration constant: as array bounds (8.3.4, 5.3.4), as case expressions (6.4.2), as bit-field lengths (9.6), as enumerator initializers (7.2), as static member initializers (9.4.2), and as integral or enumeration non-type template arguments (14.3). constant-expression: conditional-expression An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions. Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types. Only type conversions to integral or enumeration types can be used. In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used.

int a = 100 is not a constant expression according to this definition.

Andreas Brinck
+9  A: 

All compilers implement the C++ standard in subtly different ways. However, the problems you are getting with g++ are because by default it enables lots of language extensions. To get warnings about these, you should always compile with at least the -Wall and -pedantic flags:

g++ -Wall -pedantic myfile.cpp

which will give the following errors/warnings:

myfile.cpp:1:119: error: iostream.h: No such file or directory
myfile.cpp:2: error: '::main' must return 'int'
myfile.cpp: In function 'int main()':
myfile.cpp:6: warning: ISO C++ forbids variable length array 'arr'
myfile.cpp:9: error: 'cout' was not declared in this scope
anon
Even without the -Wall or -pedantic, I get a warning that the X.h header is deprecated, with the <X> I get the cout not defined errors, as you would expect, as the <X.h> version uses the global namespace not std.
Chris Huang-Leaver