scope rules for "for loop"

S

Shadyabhi

#include<iostream>
using namespace std;
int main()
{
for (int i=0;i<1;i++)
{
cout<<i;
int i=7;
cout<<i;
}
return 0;
}

The above code gives output: 07

How come is the output that? When i run this program through GDB, i
found out that both these variables have same address.

TERMINAL OUTPUT WHILE USING GDB:

(gdb) break 8
Breakpoint 1 at 0x40093e: file test.cpp, line 8.
(gdb) break 9
Breakpoint 2 at 0x400945: file test.cpp, line 9.
(gdb) r
Starting program: /home/codefire/prgs/a.out

Breakpoint 1, main () at test.cpp:8
8 int i=7;
(gdb) p i
$1 = 0
(gdb) p &i
$2 = (int *) 0x7fff3323d628
(gdb) n

Breakpoint 2, main () at test.cpp:9
9 cout<<i;
(gdb) p i
$3 = 7
(gdb) p &i
$4 = (int *) 0x7fff3323d628
(gdb)


As far as i can say, it should have been error of redeclaration of i.
Please let me know of whats happening.
 
B

Balog Pal

Shadyabhi said:
#include<iostream>
using namespace std;
int main()
{
for (int i=0;i<1;i++)
{
cout<<i;
int i=7;
cout<<i;
}
return 0;
}

Cameau reports:
"ComeauTest.c", line 8: error: "i", declared in for-loop initialization, may
not be
redeclared in this scope
int i=7;

Just as expected.
How come is the output that? When i run this program through GDB, i
found out that both these variables have same address.

You shall not really use gdb for that purpose -- it may mis-report the
stuff. If you're interested in address, take it in code and write it out. Or
look at the assy.

Some compilers have switches to adjust the for scoping, you have to look
which is the 'default' or what switches imly which...
 
S

Shadyabhi

Cameau reports:
"ComeauTest.c", line 8: error: "i", declared in for-loopinitialization, may
not be
          redeclared in thisscope
  int i=7;

Just as expected.


You shall not really use gdb for that purpose -- it may mis-report the
stuff. If you're interested in address, take it in code and write it out. Or
look at the assy.

Some compilers have switches to adjust the for scoping, you have to look
which is the 'default' or what switches imly which...

I used GCC compiler which i suppose is a standard compiler and it did
not show any error. Why?
 
P

paulkp

I used GCC compiler which i suppose is a standard compiler and it did
not show any error. Why?

The error was too stupid or GCC sucks. Maybe a bit of both.
 
L

Lionel B

I used GCC compiler which i suppose is a standard compiler

It *can* be (insofar as any compiler is standards compliant) - but note
that *by default* it does not actually enforce the current C++ standard,
but adds some extensions (see the GCC docs for details).
and it did not show any error. Why?

I can confirm this for g++ (4.3.2) with switches

-std=c++98 -pedantic -Wall -Wextra

which should enforce compliance with current standard. Not a peep, no
warnings, outputs '07'.

This does look rather like g++ is at fault here (I suspect Comeau is
correct).
 
J

James Kanze

I couldn't reproduce that. Both g++ and Sun CC accept the code,
treating i as a redeclaration, outputting 0, then 7.
I used GCC compiler which i suppose is a standard compiler and
it did not show any error. Why?

It's a bug. It seems to be a frequent one, since it affects two
of the three compilers I have access to (g++ and Sun CC---VC++
gets it right).
 
J

James Kanze

[...]
I can confirm this for g++ (4.3.2) with switches
-std=c++98 -pedantic -Wall -Wextra
which should enforce compliance with current standard. Not a
peep, no warnings, outputs '07'.
This does look rather like g++ is at fault here (I suspect
Comeau is correct).

It is. I checked the standard. If you really want to stretch
it, I guess you could argue that the normative wording is
ambiguous, but that would require a lot of stretching. And there
is an example (non-normative) of exactly this, stating that it
is an error. So it's an error in g++.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top