public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/47686] New: scope of variables declared within a nested for loop is trivial
@ 2011-02-10 19:41 chalacy at sonalysts dot com
  2011-02-10 19:42 ` [Bug c++/47686] " redi at gcc dot gnu.org
  2011-02-10 22:41 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: chalacy at sonalysts dot com @ 2011-02-10 19:41 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47686

           Summary: scope of variables declared within a nested for loop
                    is trivial
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: chalacy@sonalysts.com


I am using gcc v. 4.1.2 on red hat enterprise linux x86_64 (64 bit) kernel:
2.6.18-238.1.1el5.  Declaring a variable within a nested for loop causes the
variable to become static when uninitialized.  for example:

for(int i =0; i < 5; i++) {
   int x;
   for(int j = 0; j < 5; j++ {
      x += i * j;
   }
}
The first iteration of the outer loop would result in x having a value of 15.
The second iteration of the loop would cause the "newly" declared x to hold on
to its value of 15 and continue to add on to that value based on the inner
loop.  Now I know good coding conduct would be to declare that accumulator
variable outside of the looping structure or to at least initialize the x to 0
or some arbitrary number each iteration.  From what I saw, using my debugger
(gdb) it shows that the "newly" declared variable is using the same memory
block every single iteration but never clearing the value within that memory
block.  The first uninitialized declaration of the x integer will produce a
debugger output of some arbitrary value assigned by the debugger.  It does not
seem right to have the x integer in the outer loop act like a static variable.

Any input on this would be greatly appreciated!


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/47686] scope of variables declared within a nested for loop is trivial
  2011-02-10 19:41 [Bug c++/47686] New: scope of variables declared within a nested for loop is trivial chalacy at sonalysts dot com
@ 2011-02-10 19:42 ` redi at gcc dot gnu.org
  2011-02-10 22:41 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-10 19:42 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47686

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-02-10 19:41:12 UTC ---
the code has undefined behaviour


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/47686] scope of variables declared within a nested for loop is trivial
  2011-02-10 19:41 [Bug c++/47686] New: scope of variables declared within a nested for loop is trivial chalacy at sonalysts dot com
  2011-02-10 19:42 ` [Bug c++/47686] " redi at gcc dot gnu.org
@ 2011-02-10 22:41 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-10 22:41 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47686

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-02-10 21:30:57 UTC ---
Of course the same memory location is used, it's at a predictable location on
the stack. If you want it to be cleared, then initialize it, don't expect the
compiler to do so.

If you want code that 'seems right' then write code that conforms to the rules
of the language.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-02-10 21:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-10 19:41 [Bug c++/47686] New: scope of variables declared within a nested for loop is trivial chalacy at sonalysts dot com
2011-02-10 19:42 ` [Bug c++/47686] " redi at gcc dot gnu.org
2011-02-10 22:41 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).