Thread: Unexpected behaviour

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by fnoyan
    Hi
    The following code must give error but it does not! I use gcc 3.3.4 under Slackware Linux.

    What is the reason of this?
    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.

    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
    Last edited by Dave Evans; 03-05-2005 at 09:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected variable behaviour
    By Dondrei in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:11 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Unexpected behaviour with float
    By j.sreejit in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 09:53 PM
  4. unexpected 'class Window (' ??? i dont understand the error
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 10:12 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM