Topic: scope in while()


Author: Valentin.Bonnard@free.fr (Valentin Bonnard)
Date: Sun, 14 Oct 2001 22:08:48 GMT
Raw View
"Balog Pal"  wrote:
> Which scope should be used in  while?
>
> void func()
> {
>   int foo = 1;
>
>   while(foo)
>   {
>     bar();
>     int foo = 0;
>   }
> }
>
> MSVC compiles the above, and uses the outside foo in the expression (so
> that makes an infinite loop).  [But as a bonus the debugger shows the
> value of the inner foo so confusing the programmer... :]

(Text reformated -- please write short lines (no more than
72 characters).)

As others have pointed out, the lifetime of the second
foo ends at the first }.

If you want to test the second foo, do it explicitely,
while it is in scope:

void func()
{
 loop:
  {
    bar();
    int foo = 0;
    if (foo)
      goto loop;
  }
}

has the semantic you want.

  --   VB

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Balog Pal" <pasa@lib.hu>
Date: Tue, 9 Oct 2001 21:02:13 GMT
Raw View
Which scope should be used in  while?

void func()
{
  int foo = 1;

  while(foo)
  {
    bar();
    int foo = 0;
  }
}

MSVC compiles the above, and uses the outside foo in the expression (so that makes an infinite loop).  [But as a bonus the debugger shows the value of the inner foo so confusing the programmer... :]

Paul

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Wed, 10 Oct 2001 17:50:58 GMT
Raw View

Balog Pal wrote:
>
> Which scope should be used in  while?
>
> void func()
> {
>   int foo = 1;
>
>   while(foo)
>   {
>     bar();
>     int foo = 0;
>   }
> }
>
> MSVC compiles the above, and uses the outside foo in the expression (so that makes an infinite loop).  [But as a bonus the debugger shows the value of the inner foo so confusing the programmer... :]

The condition expression in the while statement uses the foo you initialized to 1.
The scope of the statement attached to the while (containing the foo initialized to 0)
doesn't even exist when the condition is tested.

You do know that each time the statement attached to the while executes you have
a different instance of the auto variables decleared therein?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 10 Oct 2001 18:21:53 GMT
Raw View
Balog Pal wrote:
>
> Which scope should be used in  while?
>
> void func()
> {
>   int foo = 1;
>
>   while(foo)
>   {
>     bar();
>     int foo = 0;
>   }
> }
>
> MSVC compiles the above, and uses the outside foo in the expression (so that makes an infinite loop).  [But as a bonus the debugger shows the value of the inner foo so confusing the programmer... :]

The condition of the loop is inside the scope of the outer foo, and is
not in the scope of the inner foo.  Therefore, MSVC is handling this
code correctly.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "rcc" <cox@oti-hsv.com>
Date: Wed, 10 Oct 2001 18:25:33 GMT
Raw View
Paul:
   Your second declaration of "foo" only exists within the
block delimited by the "{" and "}" of the "while" statement.
The decision expression of the "while" statement refers to
the "foo" that it can see within its scope.




Some relevant refences from ISO 14882
3.3 Declarative regions and scopes,  p. 24
3.3.2 Local Scope, p. 26


3.3.2, para 1 - "A name declared in a block (6.3) is local to that block.
Its potential scope begins at its
point of declaration (3.3.1) and ends at the end of its declarative region."

6.3 Compound Statement or block, p. 93
6.5.1 The while statement, p. 96

  while (decision_expression) statement

or in your case
  while (decision_expression) block_statement

good luck,
R. Cox
"Balog Pal" <pasa@lib.hu> wrote in message
news:3bc3630c@andromeda.datanet.hu...
> Which scope should be used in  while?
>
> void func()
> {
>   int foo = 1;
>
>   while(foo)
>   {
>     bar();
>     int foo = 0;
>   }
> }
>
> MSVC compiles the above, and uses the outside foo in the expression (so
that makes an infinite loop).  [But as a bonus the debugger shows the value
of the inner foo so confusing the programmer... :]
>
> Paul
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "George Sudarkoff" <gsudarko@san-jose.tt.slb.com>
Date: Wed, 10 Oct 2001 19:18:44 GMT
Raw View
"Balog Pal" <pasa@lib.hu> wrote in message
news:3bc3630c@andromeda.datanet.hu...
> Which scope should be used in  while?
>
> void func()
> {
>   int foo = 1;
>
>   while(foo)
>   {
>     bar();
>     int foo = 0;
>   }
> }
>
> MSVC compiles the above, and uses the outside foo in the expression (so
that makes an infinite loop).  [But as a bonus the debugger shows the value
of the inner foo so confusing the programmer... :]

See 3.3.2/4. And there's nothing confusing about the debugger, step outside
the loop context and you'll see the outer foo value.


--
George Sudarkoff
Sr. Software Engineer, Y.E.S.
Schlumberger Semiconductor Solutions



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 10 Oct 2001 19:21:44 GMT
Raw View
In article <3bc3630c@andromeda.datanet.hu>, Balog Pal <pasa@lib.hu>
writes
>Which scope should be used in  while?
>
>void func()
>{
>  int foo = 1;
>
>  while(foo)

at this point all that exists is the outer foo so it is used.

>  {
>    bar();
>    int foo = 0;
now you define an inner variable which happens to have the same name.
That variable immediately disappears because the iteration terminates.
Leaving only the outer one. Any other way would be insane because the
control expression would change objects after the first iteration. Now
if you really want to do something like that (I cannot imagine why but
everyone to their own choice look below:


>  }
>}

void func()
{
  int foo = 1;
  int * ptr = &foo;
  while(*ptr)
  {
    bar();
    int foo = 0;
    ptr = &foo;
  }
}
 Will seem to work (do what you expect) on most hardware but has a fatal
flaw in that ptr becomes a hanging pointer at the end of the first
iteration (it will appear to work because most platforms will recycle
the same memory for each iteration.

>
>MSVC compiles the above, and uses the outside foo in the expression (so that
>makes an infinite loop).  [But as a bonus the debugger shows the value of the
>inner foo so confusing the programmer... :]

Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]