Variable Scope

Discussion in 'C++' started by PSN, Jan 24, 2007.

  1. PSN

    PSN Guest

    The following code compiles fine with GCC. Isnt this supposed to be a
    redefinition of 'i' error ...

    main()
    {
    for (int i=0; i<10; i++)
    cout << i << endl;

    for (int i=10; i>0; i--)
    cout << i << endl;
    }

    Thanks,
    Prakash
     
    PSN, Jan 24, 2007
    #1
    1. Advertising

  2. PSN

    PSN Guest

    The MSVC++ compiler ends up with a redefinition error.

    On Jan 24, 3:17 pm, "PSN" <> wrote:
    > The following code compiles fine with GCC. Isnt this supposed to be a
    > redefinition of 'i' error ...
    >
    > main()
    > {
    > for (int i=0; i<10; i++)
    > cout << i << endl;
    >
    > for (int i=10; i>0; i--)
    > cout << i << endl;
    >
    > }Thanks,
    > Prakash
     
    PSN, Jan 24, 2007
    #2
    1. Advertising

  3. PSN wrote:
    > The following code compiles fine with GCC. Isnt this supposed to be a
    > redefinition of 'i' error ...
    >
    > main()
    > {
    > for (int i=0; i<10; i++)
    > cout << i << endl;
    >
    > for (int i=10; i>0; i--)
    > cout << i << endl;
    > }


    No. That's the rule for variables declared in the 'for' statement.
    Before it was standardized that way the language went back and forth
    a couple of times briefly, and some compilers (VC++) implemented it
    the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
    and perhaps you've got used to the incorrect 'for' variables rule...
    It's time to _un-learn_ bad habits. :)

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 24, 2007
    #3
  4. PSN

    Ondra Holub Guest

    PSN napsal:
    > The following code compiles fine with GCC. Isnt this supposed to be a
    > redefinition of 'i' error ...
    >
    > main()
    > {
    > for (int i=0; i<10; i++)
    > cout << i << endl;
    >
    > for (int i=10; i>0; i--)
    > cout << i << endl;
    > }
    >
    > Thanks,
    > Prakash


    AFAIK it was changed (I do not how many years ago) in standard, so that
    'i' is valid only within loop. In some howto for Mozilla (how to write
    portable code) was mentioned, that some compilers will not compile your
    example. But I think it is correct acording to standard (it is not
    redefinition).
     
    Ondra Holub, Jan 24, 2007
    #4
  5. PSN

    Ondra Holub Guest

    PSN napsal:
    > The following code compiles fine with GCC. Isnt this supposed to be a
    > redefinition of 'i' error ...
    >
    > main()
    > {
    > for (int i=0; i<10; i++)
    > cout << i << endl;
    >
    > for (int i=10; i>0; i--)
    > cout << i << endl;
    > }
    >
    > Thanks,
    > Prakash


    AFAIK it was changed (I do not how many years ago) in standard, so that
    'i' is valid only within loop. In some howto for Mozilla (how to write
    portable code) was mentioned, that some compilers will not compile your
    example. But I think it is correct acording to standard (it is not
    redefinition).
     
    Ondra Holub, Jan 24, 2007
    #5
  6. PSN

    mlimber Guest

    On Jan 24, 9:19 am, "PSN" <> wrote:
    > On Jan 24, 3:17 pm, "PSN" <> wrote:
    > > The following code compiles fine with GCC. Isnt this supposed to be a
    > > redefinition of 'i' error ...

    >
    > > main()
    > > {
    > > for (int i=0; i<10; i++)
    > > cout << i << endl;

    >
    > > for (int i=10; i>0; i--)
    > > cout << i << endl;

    >
    > > }

    >
    > The MSVC++ compiler ends up with a redefinition error.


    VC6 is not conformant on this point (MSDN has a note on this). The
    standard allows it, as do all C++ compilers released in the last decade
    or so.

    Cheers! --M
     
    mlimber, Jan 24, 2007
    #6
  7. PSN

    *PaN!* Guest

    "Victor Bazarov" <> wrote in message
    news:ep7q17$31s$...
    > PSN wrote:
    >> The following code compiles fine with GCC. Isnt this supposed to be a
    >> redefinition of 'i' error ...
    >>
    >> main()
    >> {
    >> for (int i=0; i<10; i++)
    >> cout << i << endl;
    >>
    >> for (int i=10; i>0; i--)
    >> cout << i << endl;
    >> }

    >
    > No. That's the rule for variables declared in the 'for' statement.
    > Before it was standardized that way the language went back and forth
    > a couple of times briefly, and some compilers (VC++) implemented it
    > the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
    > and perhaps you've got used to the incorrect 'for' variables rule...
    > It's time to _un-learn_ bad habits. :)


    btw, most of the people I know use a workaround for VC++ 6, that is
    re-defining for as << if(0);else for >> (it can be easily set as a
    project-level compiler switch).

    --
    Marco
     
    *PaN!*, Jan 24, 2007
    #7
  8. *PaN!* wrote:
    > "Victor Bazarov" <> wrote in message
    > news:ep7q17$31s$...
    >> PSN wrote:
    >>> The following code compiles fine with GCC. Isnt this supposed to be
    >>> a redefinition of 'i' error ...
    >>>
    >>> main()
    >>> {
    >>> for (int i=0; i<10; i++)
    >>> cout << i << endl;
    >>>
    >>> for (int i=10; i>0; i--)
    >>> cout << i << endl;
    >>> }

    >>
    >> No. That's the rule for variables declared in the 'for' statement.
    >> Before it was standardized that way the language went back and forth
    >> a couple of times briefly, and some compilers (VC++) implemented it
    >> the "wrong" way, and held onto that. VC++ v6 is pre-standard
    >> compiler and perhaps you've got used to the incorrect 'for'
    >> variables rule... It's time to _un-learn_ bad habits. :)

    >
    > btw, most of the people I know use a workaround for VC++ 6, that is
    > re-defining for as << if(0);else for >> (it can be easily set as a
    > project-level compiler switch).


    It's an ugly hack, and is against the Standard as well, BTW. Of
    course keeping using VC++ v6 nowadays is a punishable offence (or
    it should be :))

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 24, 2007
    #8
  9. PSN

    Pete Becker Guest

    *PaN!* wrote:
    >
    > btw, most of the people I know use a workaround for VC++ 6, that is
    > re-defining for as << if(0);else for >> (it can be easily set as a
    > project-level compiler switch).
    >


    This introduces undefined behavior if you're not extremely careful. In
    every situation I've seen where the same name was used for the loop
    variable in multiple for loops, the simplest workaround was to hoist the
    declaration of the variable into the enclosing block:

    int main()
    {
    for (int i = 0; i < 10; ++i)
    ;
    for (int i = 0; i < 10; ++i)
    ;
    return 0;
    }

    becomes

    int main()
    {
    int i;
    for (i = 0; i < 10; ++i)
    ;
    for (i = 0; i < 10; ++i)
    ;
    return 0;
    }

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Jan 24, 2007
    #9
  10. PSN

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > VC6 is not conformant on this point (MSDN has a note on this). The
    > standard allows it, as do all C++ compilers released in the last decade
    > or so.


    It's mostly a technicality, but VC++ 6 _can_ conform in this regard --
    if you ask it for its best conformance with the standard (/Za) it gives
    variables declared in for loops the correct scope.

    As I said, this is mostly a technicality though: when you do this, it
    also enforces a couple of other rules that prevent it from compiling
    nearly _any_ of its own headers, so the ability is generally useless
    even though it's present.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jan 24, 2007
    #10
  11. PSN

    *PaN!* Guest

    "Pete Becker" <> wrote in message
    news:...

    >> btw, most of the people I know use a workaround for VC++ 6, that is
    >> re-defining for as << if(0);else for >> (it can be easily set as a
    >> project-level compiler switch).

    > This introduces undefined behavior if you're not extremely careful.


    I never trusted that solution very much (and it's really ugly), but can't
    see how an undefined behaviour can arise (except if someone else is already
    redefining for).

    > In every situation I've seen where the same name was used for the loop
    > variable in multiple for loops, the simplest workaround was to hoist the
    > declaration of the variable into the enclosing block:


    Indeed, my favourite solution is modifying the code.

    > int main()
    > {
    > for (int i = 0; i < 10; ++i)
    > ;
    > for (int i = 0; i < 10; ++i)
    > ;
    > return 0;
    > }
    >
    > becomes
    >
    > int main()
    > {
    > int i;
    > for (i = 0; i < 10; ++i)
    > ;
    > for (i = 0; i < 10; ++i)
    > ;
    > return 0;
    > }


    My favourite solution is actually

    int main()
    {
    {
    for (int i = 0; i < 10; ++i)
    ;
    }
    {
    for (int i = 0; i < 10; ++i)
    ;
    }
    }

    since while refactoring the code it might happen

    a) that the two loops have different types for the i variable (e.g. int,
    unsigned int)

    b) that the two loops are in a longer/complex function

    so I found that a simpler/faster solution is to enclose the for-loops in a
    block.

    --
    Marco
     
    *PaN!*, Jan 24, 2007
    #11
  12. PSN

    Ron Natalie Guest


    > No. That's the rule for variables declared in the 'for' statement.
    > Before it was standardized that way the language went back and forth
    > a couple of times briefly, and some compilers (VC++) implemented it
    > the "wrong" way, and held onto that. VC++ v6 is pre-standard compiler
    > and perhaps you've got used to the incorrect 'for' variables rule...
    > It's time to _un-learn_ bad habits. :)


    We fixed it with a macro in the VC6. Even the contemporaneous
    Microsoft doc's claimed it a "Microsoft Extension" before the
    standard was published. The problem in VC6 is you can't turn
    it off (i.e., get the standard behavior) without disabliing a
    lot of other "extensions" that the development environment
    relied on.

    The later versions of Visual C++ (honestly, VC6 is two major
    relases and nearly ten years old now) allow you to fix the
    for loops with an option (which I believe is the 'standard'
    way by default) distinct from anything else.
     
    Ron Natalie, Jan 25, 2007
    #12
  13. PSN

    Duane Hebert Guest

    "Ron Natalie" <> wrote in message
    news:45b8bf44$0$7432$...

    > The later versions of Visual C++ (honestly, VC6 is two major
    > relases and nearly ten years old now) allow you to fix the
    > for loops with an option (which I believe is the 'standard'
    > way by default) distinct from anything else.


    In VC7.1, at least, it's not the standard way by default
    but at least you can set it.
     
    Duane Hebert, Jan 26, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    973
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    682
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    503
  4. David Filmer
    Replies:
    19
    Views:
    256
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page