There should be a compiler error in both cases. I ran your program through g++3.4.2 in my Linux box and got both errors.Originally Posted by fnoyan
I ran the program through g++ 3.3.3 (cygwin) on my Windows XP machine and got the same results as you did: no error for the first, but a properly reported error on the second. (Borland and Microsoft Windows compilers also properly reported errors for both cases.)
For g++ 3.3.3, assembly code indicated that the value outside the loop is stored in the location of the loop-local x. Apparently the compiler didn't mind.
(Of course, the example code does nothing, and when you add program statements that access the variable outside its scope, the compiler properly reports the error. However, if this is really a compiler bug, then it is cause for concern, since it may affect other things that you would actually use in a program.)
I vaguely remember that, once upon a time, there was a gcc bug report something like this: If a variable was declared extern and another variable with the name had local scope, then after the local variable went out of scope, attempts to access the extern variable caused segfault (in some cases). Perhaps when (if) the compiler writers fixed the one problem they introduced the bug that you have seen. (Then maybe they fixed both with gcc 3.4. Maybe you can upgrade gcc to 3.4.?)
Regards,
Dave