#include #include #define LEARN_UNINITIZLIED_REAL_TYPES //main can return void or int for VC compiler //main has to return int for GCC compiler int main() { #ifdef LEARN_UNINITIALIZED_INT int iIsInitialized = 123; printf("iIsInitialized = %d\n", iIsInitialized); //Some random value auto int iautoUninitialized; //Warning C4700 from VC at compile time, exception //at run-time (run-time check failure) -858993460 //No warning from GCC, no exception at run-time: //the value is 1628438944 printf("iautoUninitialized = %d\n", iautoUninitialized); //Some random value int iUninitialized; //Warning C4700 from VC at compile time, exception //at run-time (run-time check failure) -858993460 //No warning from GCC, no exception at run-time: //the value is 0 (after we call the previous auto //value it becomes zero) printf("iUninitialized = %d\n", iUninitialized); #endif #ifdef LEARN_UNINITIZLIED_POINTERS //C4700 from VC compiler, First run-time check //failure that can be supassed but after that //we get access violation exception //that terminates the application //GCC doesn't complain and actually runs //it and shows some value auto int* myIninitializePointerToInt; printf("myIninitializePointerToInt = %d\n", *myIninitializePointerToInt); #endif #ifdef LEARN_UNINITIZLIED_REAL_TYPES //Unlike integer ojbects, real number objects //can contain values that correspond to //no genuine number like NAN //NAN (Not a number), math.h has especial //implementation defined values corresponding //to no actual real numbers. float fNAN = log((float)-2); //VCC: -1.#IND00 //GCC: -inf printf("fNAN = %f\n", fNAN); #endif return 0; } //For GCC compiler there must be a new line //at the end of file
Friday, July 16, 2010
How VC++ and G++ treat uninitialized variables
Here is a sample code that demonstrates a basic C++ application and the different behavior represented by Visual C++ and GCC compilers regrading uninitialized variables
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment