473,725 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

redefinition inside for loop

On my visual c++ compiler, I compiled code which contained something
like

for( int i =0; i < 5; i++)

{ double x =5;}

I expected it to give a compiler error because x is being redefined
and redeclared each time through the loop.

Is there an ANSI standard that says that you can redefine variables
inside a for-loop?

Or is this regarded as not being a redefinition because the definition
is only written once?

I'd be grateful if someone could explain why the above code compiles
but {//some code
double x=5; double x=6; // more code} gives a redefinition error.

Thank you,

Paul Epstein

Oct 25 '07 #1
9 7523
pa**********@at t.net wrote:
On my visual c++ compiler, I compiled code which contained something
like

for( int i =0; i < 5; i++)

{ double x =5;}

I expected it to give a compiler error because x is being redefined
and redeclared each time through the loop.
Why do you consider that a problem?
Is there an ANSI standard that says that you can redefine variables
inside a for-loop?
What do you mean by "redefine"? You define it only once. it gets created at
the point of definition and destroyed at the end of the scope, just like
any other (non-static) local variable. So on each iteration it gets created
and destroyed.
Or is this regarded as not being a redefinition because the definition
is only written once?

I'd be grateful if someone could explain why the above code compiles
but {//some code
double x=5; double x=6; // more code} gives a redefinition error.
Because you define two variables with the same name in the same scope. In
your for loop, you don't have that.

Oct 25 '07 #2
On 2007-10-25 05:54, pa**********@at t.net wrote:
On my visual c++ compiler, I compiled code which contained something
like

for( int i =0; i < 5; i++)

{ double x =5;}

I expected it to give a compiler error because x is being redefined
and redeclared each time through the loop.

Is there an ANSI standard that says that you can redefine variables
inside a for-loop?
No, but there is one that allows you to redefine a variable in a new scope.
Or is this regarded as not being a redefinition because the definition
is only written once?

I'd be grateful if someone could explain why the above code compiles
but {//some code
double x=5; double x=6; // more code} gives a redefinition error.
You can not have two variables with the same name in the same scope.

--
Erik Wikström
Oct 25 '07 #3
On Oct 25, 4:54 am, pauldepst...@at t.net wrote:
On my visual c++ compiler, I compiled code which contained something
like

for( int i =0; i < 5; i++)

{ double x =5;}

I expected it to give a compiler error because x is being redefined
and redeclared each time through the loop.

Is there an ANSI standard that says that you can redefine variables
inside a for-loop?

Or is this regarded as not being a redefinition because the definition
is only written once?

I'd be grateful if someone could explain why the above code compiles
but {//some code
double x=5; double x=6; // more code} gives a redefinition error.

Thank you,

Paul Epstein
x is created within the scope of the loop. in your case it's put on
the stack. At the end of the loop, the stack unwinds and it gets
removed. You then start through the loop again and create a new
variable called x on the stack. It uses the same slot in the stack but
is still a different object.

Consider:

for (int i = 0; i < 5; ++i)
{
double* x = new double(5);
delete x;
}

This does the same thing as your code but on the heap so it might be a
little clearer.

Oct 25 '07 #4
On Oct 25, 9:48 am, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 05:54, pauldepst...@at t.net wrote:
On my visual c++ compiler, I compiled code which contained something
like
for( int i =0; i < 5; i++)
{ double x =5;}
I expected it to give a compiler error because x is being redefined
and redeclared each time through the loop.
Is there an ANSI standard that says that you can redefine
variables inside a for-loop?
No, but there is one that allows you to redefine a variable in
a new scope.
It's a bit trickier than that; in this case, I think that the
for introduces a new scope. Think of it for a moment:

for ( int i = 0 ; i < 10 ; ++ i )
MyClassType c ;

If the for doesn't introduce a new scope, where does c get
destructed? (In the early days, this wasn't that clear, and a
lot of compilers would call the destructor of c at the next }.
Even if the conditions of the for loop meant that it was never
executed.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 25 '07 #5
On Oct 25, 6:54 am, pauldepst...@at t.net wrote:
On my visual c++ compiler, I compiled code which contained something
like

for( int i =0; i < 5; i++)

{ double x =5;}

I expected it to give a compiler error because x is being redefined
and redeclared each time through the loop.

Is there an ANSI standard that says that you can redefine variables
inside a for-loop?

Or is this regarded as not being a redefinition because the definition
is only written once?

I'd be grateful if someone could explain why the above code compiles
but {//some code
double x=5; double x=6; // more code} gives a redefinition error.
let us call every pair of { and } a block statment.then any acceptable
identifier can be defined exactly once in each block statement.A block
nested in another block can access the identifiers defined in the
nesting block or hide them via redefining them .

regards,
FM.

Oct 25 '07 #6
James Kanze wrote:
Is there an ANSI standard that says that you can redefine
variables inside a for-loop?
>No, but there is one that allows you to redefine a variable in
a new scope.

It's a bit trickier than that; in this case, I think that the
for introduces a new scope. Think of it for a moment:

for ( int i = 0 ; i < 10 ; ++ i )
MyClassType c ;

If the for doesn't introduce a new scope, where does c get
destructed?
Same for the variable i.

Oct 25 '07 #7
On Oct 25, 6:47 pm, Rolf Magnus <ramag...@t-online.dewrote:
James Kanze wrote:
Is there an ANSI standard that says that you can redefine
variables inside a for-loop?
No, but there is one that allows you to redefine a variable in
a new scope.
It's a bit trickier than that; in this case, I think that the
for introduces a new scope. Think of it for a moment:
for ( int i = 0 ; i < 10 ; ++ i )
MyClassType c ;
If the for doesn't introduce a new scope, where does c get
destructed?
Same for the variable i.
Yes. But that's also an intentional change.

As far as I can tell, early (pre-standard) C++ didn't really
specify the lifetime of something like c, above. Generally
speaking, the rule was that the variable had a scope until the
end of the block, which would (logically) mean:

for ( int i = 0 ; i < 10 ; i ++ )
MyClassType c ;
c.doSomething() ; // c still valid here !!!

IIRC, many early compilers did allow this, calling the
constructor of c each time through the loop (and not at all if
the loop executed 0 times), and calling the destructor once at
the end of the enclosing block. As you can imagine, this didn't
work out too well in practice. (IIRC, too, CFront---which only
destructed temporaries at the end of the block---did the same
thing with temporaries. Including any generated in condition or
the increment parts of the for. I definitely once had a problem
with a temporary returned by the increment part. And while
something like the above is really unlikely in actual code,
temporaries aren't.)

Because of the obvious problems, the rule was very quickly
changed that objects declared in a conditional statement had the
scope of that statement; i.e. c, above, had the scope of the for
statement. (I think this change was even before standardization
began---in fact, it may have been the rule from the start, and
just have been poorly implemented by the compilers I was using.)

When dynamic_cast was introduced, the grammar was modified to
allow a declaration in the condition; this declaration was given
the scope of the statement. Only after that was it felt that
this made the longer scope of the variable in the initialization
part of the for inconsistent, so its scope was changed. (There
had been a proposal earlier to change it; at that time, it was
generally agreed that the scope should have been that of the for
statement, but that changing it at that time would involve
breaking too much code. By the time dynamic_cast was added,
however, the dynamics of the committee had changed, and nobody
cared about existing code.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 26 '07 #8
On Oct 26, 11:16 am, James Kanze <james.ka...@gm ail.comwrote:
On Oct 25, 6:47 pm, Rolf Magnus <ramag...@t-online.dewrote:
James Kanze wrote:
Is there an ANSI standard that says that you can redefine
variables inside a for-loop?
>No, but there is one that allows you to redefine a variable in
>a new scope.
It's a bit trickier than that; in this case, I think that the
for introduces a new scope. Think of it for a moment:
for ( int i = 0 ; i < 10 ; ++ i )
MyClassType c ;
If the for doesn't introduce a new scope, where does c get
destructed?
Same for the variable i.

Yes. But that's also an intentional change.

As far as I can tell, early (pre-standard) C++ didn't really
specify the lifetime of something like c, above. Generally
speaking, the rule was that the variable had a scope until the
end of the block, which would (logically) mean:

for ( int i = 0 ; i < 10 ; i ++ )
MyClassType c ;
c.doSomething() ; // c still valid here !!!

IIRC, many early compilers did allow this, calling the
constructor of c each time through the loop (and not at all if
the loop executed 0 times), and calling the destructor once at
the end of the enclosing block. As you can imagine, this didn't
work out too well in practice. (IIRC, too, CFront---which only
destructed temporaries at the end of the block---did the same
thing with temporaries. Including any generated in condition or
the increment parts of the for. I definitely once had a problem
with a temporary returned by the increment part. And while
something like the above is really unlikely in actual code,
temporaries aren't.)

Because of the obvious problems, the rule was very quickly
changed that objects declared in a conditional statement had the
scope of that statement; i.e. c, above, had the scope of the for
statement. (I think this change was even before standardization
began---in fact, it may have been the rule from the start, and
just have been poorly implemented by the compilers I was using.)

When dynamic_cast was introduced, the grammar was modified to
allow a declaration in the condition; this declaration was given
the scope of the statement. Only after that was it felt that
this made the longer scope of the variable in the initialization
part of the for inconsistent, so its scope was changed. (There
had been a proposal earlier to change it; at that time, it was
generally agreed that the scope should have been that of the for
statement, but that changing it at that time would involve
breaking too much code. By the time dynamic_cast was added,
however, the dynamics of the committee had changed, and nobody
cared about existing code.)
Sorry to terminate again;I can not see what the scope of for
initializer is .I used to take the following two as equivalent code:

_1st:
for(int init =0 ;condition ;inrement)
statement;

_2nd:
start_loop:{
int init=0;
for(;condition; increment)
{statement;};
};//destroy init

regards,
FM.

Oct 27 '07 #9
On Oct 27, 11:08 am, terminator <farid.mehr...@ gmail.comwrote:
On Oct 26, 11:16 am, James Kanze <james.ka...@gm ail.comwrote:
Sorry to terminate again ;I can not see what the scope of for
initializer is. I used to take the following two as equivalent
code:
_1st:
for(int init =0 ;condition ;inrement)
statement;
_2nd:
start_loop:{
int init=0;
for(;condition; increment)
{statement;};

};//destroy init
They are at present. They didn't used to be; in the (now fairly
distant) past, the equivalent of the first was simply:

int init=0 ;
for ( ; condition ; increment )
statement ;

I don't think that there was even an implicit {...} around the
statement, and there certainly wasn't one around the for
itself; any variables declared in the initialization part had a
lifetime corresponding to that of the block surrounding the for.
And code like:

for ( int i = 0 ; i < last && ! condition ; i ++ ) {
}
return i ;

was quite frequent.

The fact that "statement" had a scope until the end of the block
is an obvious oversight, since it cannot be made to work. It
was fixed very, very early. At more or less the same time, the
committee considered the possibility of reducing the scope of a
variable declared in the initialization part of a for. The
consensus at the time was, I think, that while it would be
cleaner, there was to much code in existence like the example
immediately above, which would break, and that such a change
would be irresponsible.

Much, much later---as part of the dynamic_cast proposal---the
possibility of defining a variable in a "condition" (if, while
or second part of a for) was introduced. After that, it was
felt that having different scopes for two variables, both
defined in a for loop, was too counter-intuitive, so the scope
of the variable in the initialization part of the for loop was
changed. (Responsibility wasn't an "in" word in the committee
at that point in time. IMHO, being able to declare a variable
in a condition is, itself, a mis-feature, but since no one is
required to use it, it doesn't bother me too much. Breaking
existing code did, but there wasn't much I could do about it.)

This change was fairly late in the standards proceedings; just
before CD1, I think. Many compilers didn't implement it
immediately, probably to avoid breaking existing code. Even
today, most compilers have an option to use the old for scope,
precisely so that old code can compile.

Note that the change can silently change the semantics of a
program. Consider:

static int i = 2 ;

int
f( int n )
{
for ( int i = 0 ; i < n ; ++ i ) {
}
return i ;
}

f(10) will return 10 under the old rules, 2 under the new. In
practice, I doubt that this is ever a problem in well written
code, since you normally wouldn't give a variable in local scope
the same name as one with namespace (or class) scope. (A good
general rule is that the length of a name is inversely
proportional to its visibility. Local variables, particularly
loop control variables, can have very, very short names, like i,
where as anything visible outside a single function requires a
somewhat longer, more meaningful name.)

FWIW: the version of g++ that I have on my machine (4.1.2) will
issue a warning for this code, by default (and use the new
rule). It also has an option (-fno-for-scope) to silently use
the old rule. (I had thought that -ffor-scope would turn off
the warning, but it doesn't seem to.)

Also, I believe that VC++ 6.0 (still widely used!) still
implemented the old rule---it did appear before the standard was
adopted.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5006
by: lomat | last post by:
Hello, While compiling a file, I get following error .... ================================= /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/type_traits.h:14 2: redefinition of `struct __type_traits<short int>' /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/type_traits.h:10 2: previous definition here =================================
13
1635
by: Gernot Frisch | last post by:
What does the standart say: for (long i=0; i<0; ++i) ; for (myclass i=0; i<0; ++i); is it allowed? i only is defined in the for-loop space, isn't it? VC6 gives me an *error*, not just a stupid warning. --
1
24331
by: squallions | last post by:
Hi I doing my c++ homework and stuck on error 'class' type redefinition. I have 5 classes. First class is base class. The next two classes are derived from the first class. The next two classes are derived from previous two classes. On the main file, I have... #include "hw6a.h" #include "hw6b.h"
9
2815
by: john hrdo | last post by:
Hey, This might well be a naive question, please be patient. Is it possible to compile a C program including two libraries lib1 and lib2 which have both a function with same name, e.g. functA? (Normally, the compiler complains about redefinition.)
4
10343
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see declaration of the function . " Could anyone please help me with this . Thank you
1
3515
by: Alex | last post by:
Hello all, I have a very stupid problem that is driving me crazy...so plz if anyone ever saw this, I would like him to help me :) I have static MFC application in MSVC++ 6.0 (named Example). That application is built by one company and its usage is to show functionality of their static libraries. Now I created dynamic link library CoreLibrary and I want to integrate it into this example. CoreLibrary worked properly with win32...
7
4233
by: Samant.Trupti | last post by:
Hi, I am getting this error. error C2371: 'LineCollection' : redefinition; different basic types I have 'LineCollection' defined in two header files in one project Like "typedef std::vector<lineLineCollection;" But both of the header file not dependant on each other, they are
5
4047
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
1
3042
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book. The basics are similar to what I'm accustomed to but there's still some confusion. I'm currently playing around with how the static, public, protected etc things work and I stumbled upon a problem. In the following copypasted code ( a tutorial I...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.