|
From: Andres R. <lo...@ho...> - 2002-04-20 13:02:13
|
Yes. Variable sized arrays are GNUC extension. If you use =96ansi or =96pedantic switches on commandline, you get only warning: =20 test.cpp: In function `int main()': test.cpp:12: warning: ANSI C++ forbids variable-size array `myArray' =20 Andres =20 -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Abhijit Shylanath Sent: Saturday, April 20, 2002 1:21 PM To: Dev-C++ User Mailing List Subject: Re: [Dev-C++] Dev-C++ Users Beware! =20 Quote Microsoft: "This isn't a bug, it's a feature." Seriously, though, this is one feature of the GCC compiler (I remember reading about it in the DJGPP docs). If you're still looking for the compiler to stop when it encounters non-ANSI code, there must be a command-line options somewhere. Yup, here it is... from the Dev-C++ help file. _____ =20 - Support all ANSI C standard programs (compiler parameter : -ansi): =20 This turns off certain features of GNU C that are incompatible with ANSI C, such as the asm, inline and typeof keywords, and predefined macros such as unix and vax that identify the type of system you are using. It also enables the undesirable and rarely used ANSI trigraph feature, and it disables recognition of C++ style // comments. _____ =20 But what the hell, GCC is kind of a standard in itself. mdeth * m.o.t.o[P.S.Y.C.H.O]=20 http://mudeth.tripod.com | ibr...@bi... | mu...@gm... ----- Original Message -----=20 From: Bob Wilson <mailto:bo...@ph...> =20 To: Dev-C++ User Mailing List <mailto:dev...@li...> =20 Sent: Saturday, April 20, 2002 3:29 AM Subject: [Dev-C++] Dev-C++ Users Beware! =20 David, An array is not dynamic, the value you pass it at initalization must be a constat of some sort.if 'i' were to change during the program some weird voodoo (the program will crash,the world will end,etc.) all this will hapen because it cannot change the size of an array at runtime(reason no. 1 linked lists were created) I am in the process of learning C++, as Ioannis well knows, and a week or so ago I was working on the array chapter. In the past I have done most of my programming in pascal, and every pascal compiler I have used will not compile an array if the size of the array is a variable, i.e., no dynamic arrays. Just fooling around I modified the simple little C++ array program to define an array with a variable. I had not done any research but was confident that the compiler would flag an error and bail out.=20 =20 Not So! the program compiled and ran allowing me to change the size of the array. I tested the bounds of the program and could build quite large arrays. the size_of function always returnd the correct size of the array. I was really surprised because that it worked. I have not employed the technique in any real world program, because I felt that it would be much safer with pointers and linked lists.=20 =20 Your message above confirmed what I suspected but had not taken the time to investigate. Stroustrup says: The number of elements of the array, the array bound, must be a constant expresseion (=A7C.5) Paragraph C.5 says in part: A constant expression evaluates to an integral of enumeration constant. Such an expression is composed of literals, enumerators, and const initialized by constant expressions. < . . . > Intuitively, constant expressions are simple expressions that can be evaluated by the compiler before the program is linked and starts to run. That tells me that the compiler should have caught this and bailed out rather than compiling, linking, and running this program:=20 =20 #include <iostream.h> #include <stdlib.h> =20 int main { int arraySize =3D 1; std::cout << "Enter the size of the array: "; std::cin >> arraySize; if (!(arraySize > 0)) { system("pause"); return 0; } int myArray[arraySize]; int arrayLength =3D sizeof(myArray)/sizeof(myArray[0]); for (int i=3D0; i < arrayLength; i++) // 0 to arrayLength-1 { myArray[i] =3D (i + 1) * 2 + 1; } for (int i=3D0; i< arrayLength; i++) // 0 to arrayLength-1 { std::cout << i << ": " << myArray[i] << "\n"; } =20 system("pause"); return 0; } The faulty compiler is the one used by Bloodshed. Visual C++ 6.0 caught the error. That is the second and most serious problem I have found with the compiler used by the dev-c++ environment. It appears it will properly compile ANSI standard code, but it will also compile code that is clearly outside of the standard.=20 =20 I believe that the dev-c++ compiler is the mingw compiler.=20 =20 Regards, =20 Bob Wilson =20 |