D
Disc Magnet
I wrote this program.
#include <iostream>
using namespace std;
int f(int a, int b)
{
int x[a];
cout << sizeof(x) << endl;
}
int test_array_size() {
f(5, 6);
}
int main()
{
test_array_size();
}
If I compile this with g++ on Linux and run it, it compiles and runs
fine. The output is: 120.
However, if I try to compile this with Visual Studio 2005, I get these
errors:
1>helloworld.cpp
1>./src/helloworld.cpp(7) : error C2057: expected constant expression
1>./src/helloworld.cpp(7) : error C2466: cannot allocate an array of
constant size 0
1>./src/helloworld.cpp(7) : error C2057: expected constant expression
1>./src/helloworld.cpp(7) : error C2466: cannot allocate an array of
constant size 0
1>./src/helloworld.cpp(7) : error C2087: 'x' : missing subscript
1>./src/helloworld.cpp(7) : error C2133: 'x' : unknown size
1>./src/helloworld.cpp(8) : error C2070: 'int [][1]': illegal sizeof
operand
1>Exception: error status 2 from C:\Program Files\Microsoft Visual
Studio 8\VC\bin\cl.exe
Could you please explain, why this difference in behavior?
#include <iostream>
using namespace std;
int f(int a, int b)
{
int x[a];
cout << sizeof(x) << endl;
}
int test_array_size() {
f(5, 6);
}
int main()
{
test_array_size();
}
If I compile this with g++ on Linux and run it, it compiles and runs
fine. The output is: 120.
However, if I try to compile this with Visual Studio 2005, I get these
errors:
1>helloworld.cpp
1>./src/helloworld.cpp(7) : error C2057: expected constant expression
1>./src/helloworld.cpp(7) : error C2466: cannot allocate an array of
constant size 0
1>./src/helloworld.cpp(7) : error C2057: expected constant expression
1>./src/helloworld.cpp(7) : error C2466: cannot allocate an array of
constant size 0
1>./src/helloworld.cpp(7) : error C2087: 'x' : missing subscript
1>./src/helloworld.cpp(7) : error C2133: 'x' : unknown size
1>./src/helloworld.cpp(8) : error C2070: 'int [][1]': illegal sizeof
operand
1>Exception: error status 2 from C:\Program Files\Microsoft Visual
Studio 8\VC\bin\cl.exe
Could you please explain, why this difference in behavior?