Topic: Which is correct??
Author: S.J.Gallacher-SE2@cs.bham.ac.uk (Steven J Gallacher)
Date: Tue, 14 Dec 1993 09:42:39 GMT Raw View
In article 131293140631@ese-31.sys.uea.ac.uk, cjp@sys.uea.ac.uk (C. Jeremy Pye) writes:
> I have run code on several compilers, and the following
> inconsitent error appears for the various compilers.
> Please reason this out for me ..
>
> function() {
>
> for (int j=0;j<10;j++)
> for (int i=0;i<10;i++) {
> do something ...
> }
>
> Is 'i' declared/in scope at this point ?
> Simply, can I write
>
> for (int i=0;i<10;i++)
> do something ..
>
> or should it be
>
> for (i=0;i<10;i++)
> do something ..
>
> return ...
> }
>
> I can't find/understand anything I have read
> in Annotated ref ... is it clear.
>
> please reply by email. Thanks in advance.
> .........................................................
> C. Jeremy Pye
> Nonlinear Image Processing Group Tel: +44 (603) 593220
> School Of Information Systems Fax: +44 (603) 507720
> UEA, Norwich, NR4 7TJ, UK Email: cjp@sys.uea.ac.uk
You cannot do :
for (int j = 0; j<10; j++)
for (int i = 0; i<10; i++)
do something...
as when j = 1, i.e. the second cycle of the outer loop, i has already been
declared, so there is a second declaration.
the way to cure this is to use teh second method. i.e
int i,j;
for (j = 0; j<10; j++)
for (i = 0; i<10; i++)
do something...
Hope this helps.
Steve.
--
A wasted youth is better by far than | The views expressed are my own and
a wise and productive old age. | are not always correct.
|
Meatloaf - Bat out of Hell II | email : sjg@cs.bham.ac.uk
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Tue, 14 Dec 1993 13:22:12 GMT Raw View
cjp@sys.uea.ac.uk (C. Jeremy Pye) writes:
>I have run code on several compilers, and the following
>inconsitent error appears for the various compilers.
>Please reason this out for me ..
>
>function() {
>
> for (int j=0;j<10;j++)
> for (int i=0;i<10;i++) {
> do something ...
> }
>
>Is 'i' declared/in scope at this point ?
No. The outermost `for' introduces an implicit scope. It's as if
you had written the following:
for (int j=0;j<10;j++)
{ // start implicit scope
for (int i=0;i<10;i++) {
do something ...
}
} // end implicit scope
But this is a relatively recent decision by the ANSI/ISO committee.
See ARM chapter 19, section 6.4. (If your ARM doesn't list it,
try a more recent one - mine is "Reprinted with corrections, May 1992.")
Some compilers may not implement the new rules yet.
--
Fergus Henderson fjh@munta.cs.mu.OZ.AU
Author: kanze@us-es.sel.de (James Kanze)
Date: 14 Dec 1993 14:25:55 GMT Raw View
In article <CI0qz3.qF@cs.bham.ac.uk> S.J.Gallacher-SE2@cs.bham.ac.uk
(Steven J Gallacher) writes:
|> In article 131293140631@ese-31.sys.uea.ac.uk, cjp@sys.uea.ac.uk (C. Jeremy Pye) writes:
|> > I have run code on several compilers, and the following
|> > inconsitent error appears for the various compilers.
|> > Please reason this out for me ..
|> > function() {
|> > for (int j=0;j<10;j++)
|> > for (int i=0;i<10;i++) {
|> > do something ...
|> > }
|> > Is 'i' declared/in scope at this point ?
|> > Simply, can I write
|> > for (int i=0;i<10;i++)
|> > do something ..
|> > or should it be
|> > for (i=0;i<10;i++)
|> > do something ..
|> > return ...
|> > }
|> > I can't find/understand anything I have read
|> > in Annotated ref ... is it clear.
I don't think that this is in the ARM (but I may be wrong), but there
is a generally accepted rule which has been adapted by the standards
committee: the scope of the inner declaration terminates at the end of
the enclosing 'for'. More generally, *all* declarations controlled by
a conditional expression (while, the second part of the for, if) have
a scope which ends at the end of the condition, thus:
if ( x )
for ( int i = ...
or
while ( x )
for ( int i = ...
In both cases, the 'i' goes out of scope at the end of the enclosing
if/while statement.
Note that of the compilers I have tried, none of those which did not
already implement this rule did anything that could possibly be
considered legal in the presence of a type with constructors and
destructors.
|> You cannot do :
|> for (int j = 0; j<10; j++)
|> for (int i = 0; i<10; i++)
|> do something...
|> as when j = 1, i.e. the second cycle of the outer loop, i has already been
|> declared, so there is a second declaration.
As pointed out above, this is not correct. However, the reason given
is precisely the rationale for the special rule. Try changing the
type of 'i' to a type which has both a constructor and a destructor,
and see when each would otherwise get called.
|> the way to cure this is to use teh second method. i.e
|> int i,j;
|> for (j = 0; j<10; j++)
|> for (i = 0; i<10; i++)
|> do something...
This is of course legal, but goes against the general rule of always
initializing in the declaration.
--
James Kanze email: kanze@us-es.sel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Author: steve@maths.warwick.ac.uk (Steve Rumsby)
Date: Tue, 14 Dec 1993 16:32:06 GMT Raw View
cjp@sys.uea.ac.uk (C. Jeremy Pye) writes:
>
>I have run code on several compilers, and the following
>inconsitent error appears for the various compilers.
>Please reason this out for me ..
>
>function() {
>
> for (int j=0;j<10;j++)
> for (int i=0;i<10;i++) {
> do something ...
> }
>
>Is 'i' declared/in scope at this point ?
>
No, but 'j' is. The outer loop introduces a new scope for its body, even
though there are no braces.
>I can't find/understand anything I have read
>in Annotated ref ... is it clear.
>
Look at the for/while equivalence rule at the top of ARM 6.5.3. This says
that the above code is equivalent to:
int j=0;
while(j<10) {
int i=0;
while(i<10) {
do something ...
i++;
}
j++
}
All of the above braces are a result of the re-writing rule, and make it
clear that a new scope is introduced for the loop body.
Steve.
--
UUCP: ...!uknet!warwick!steve Internet: steve@maths.warwick.ac.uk
JANET: steve@uk.ac.warwick.maths PHONE: +44 203 524657
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Tue, 14 Dec 1993 12:22:56 GMT Raw View
In article <cjp-131293140631@ese-31.sys.uea.ac.uk> cjp@sys.uea.ac.uk (C. Jeremy Pye) writes:
>I have run code on several compilers, and the following
>inconsitent error appears for the various compilers.
>Please reason this out for me ..
From what?
>
>function() {
>
> for (int j=0;j<10;j++)
> for (int i=0;i<10;i++) {
> do something ...
> }
>
>Is 'i' declared/in scope at this point ?
As it currently stands (in at least one place in the Working Paper),
the condition and body of a for statement are nested
(in an implied inner scope), whereas the for-init
statement is not. Thus, i is not in scope but j is.
(another place was not updated correctly, and should reflect the
above rules)
There is a proposal to change this so that
the j is not in scope where you ask either.
Dont ask whether this is legal:
if(int i=1){int i=1; i++;}
[technically, it is, as far as I can determine, but its
subject to interpretation and may not have been the
committee's intent.]
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA
Author: cjp@sys.uea.ac.uk (C. Jeremy Pye)
Date: Mon, 13 Dec 1993 14:16:25 GMT Raw View
I have run code on several compilers, and the following
inconsitent error appears for the various compilers.
Please reason this out for me ..
function() {
for (int j=0;j<10;j++)
for (int i=0;i<10;i++) {
do something ...
}
Is 'i' declared/in scope at this point ?
Simply, can I write
for (int i=0;i<10;i++)
do something ..
or should it be
for (i=0;i<10;i++)
do something ..
return ...
}
I can't find/understand anything I have read
in Annotated ref ... is it clear.
please reply by email. Thanks in advance.
........................................................
C. Jeremy Pye
Nonlinear Image Processing Group Tel: +44 (603) 593220
School Of Information Systems Fax: +44 (603) 507720
UEA, Norwich, NR4 7TJ, UK Email: cjp@sys.uea.ac.uk