g++ allows variable array creation
Omar Shaikh
omarshaikh71@yahoo.com
Mon Jan 19 11:31:00 GMT 2004
Dear All
I found something in the GNU compiler that I am not
sure is a bug or the latest change to the C++
standard. However, the same did not work in Microsoft
C++ (v. 13.10.3077), the C++ compiler that comes with
Microsoft .NET product. I ran this code on both Win32
and Linux platforms with full warnings on (-Wall).
Surprisingly, there were no warnings either.
The compiler that I used on Windows (XP):
MinGW: c++ (GCC) 3.2.3 (mingw special 20030504-1)
Compiler used on (Mandrake) Linux:
g++ (GCC) 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)
#include <iostream.h>
class A
{
int v;
public:
A () { }
void SetV (int nv) { v = nv; }
int GetV () { return v; }
};
int main ()
{
A x;
int i;
cout << "Enter array size: "; cin >> i;
x.SetV(i);
const int m = x.GetV();
int arr[m]; // Works fine!
cout << "Array size = " << sizeof(arr) << " bytes" <<
endl;
return 0;
}
Here are two sample runs of the program. (Underscored
text is user i/p.)
Enter array size: _10_
Array size = 40 bytes
Next run
Enter array size: _25_
Array size = 100 bytes
The code above works fine, though the array size can
be decided during runtime. Please let me know if I am
missing something.
Thank you.
Omar
__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus
More information about the Gcc-bugs
mailing list