From: Stephen Leake <Stephe.Leake@nasa.gov>
Subject: Re: ada loops
Date: 30 May 2003 13:25:31 -0400
Date: 2003-05-30T17:41:34+00:00 [thread overview]
Message-ID: <uvfvsmjn8.fsf@nasa.gov> (raw)
In-Reply-To: d3ba4d4c.0305300744.520de992@posting.google.com
rm@gunlab.com.ru (Roman V. Isaev) writes:
> I've got bitten by this (code was translated from pascal by p2ada, so
> please don't bash using goto):
>
> for Ijk in 1 .. 40 loop
> Sss:=Fnmis-Si(Ijk);
> if Sss<0.0 then
> goto Label_1;
> end if;
> end loop; -- IJK
> <<Label_1>> null;
>
> Before the loop Ijk is 0, after the loop Ijk is still 0. Huh? Why?
You left out the first declaration of Ijk. You must actually have
something like:
procedure Foo is
Ijk : Integer;
begin
for Ijk in 1 .. 40 loop
Sss:=Fnmis-Si(Ijk);
if Sss<0.0 then
goto Label_1;
end if;
end loop; -- IJK
<<Label_1>> null;
end Foo;
There are _two_ variables named Ijk; one in the Foo procedure, and one
in the loop. The loop index variable is _not_ visible outside the
loop. Inside the loop, it hides the outer declaration.
I don't remember how Pascal did this; I suspect the loop variable was
not a new declaration. In standard C++, the loop variable is a new
declaration (as in Ada), but in early versions of C++, it wasn't.
> I replaced Ijk with temp variable and put Ijk := temp before goto
> and it solved the problem, but why we can't have final value when we
> break out of the loop? What's the logic behind this language
> behaviour?
I'm not sure of all of the reasons (see the Ada Rationale for more).
It may have to do with optimizations.
> Also when I try to debug this in GVD I can see Ijk variable, but not
> Sss (it's a global variable described in outer procedure). I see
> following message in the bottom window:
>
> (gdb) print Sss
> Cannot access memory at address 0x24803ec
That means you have a bug in your code, which is clobbering the stack
or something. Or you have a version of gdb that is not Ada-aware. What
OS and compiler are you using?
--
-- Stephe
next prev parent reply other threads:[~2003-05-30 17:25 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-05-30 15:44 ada loops Roman V. Isaev
2003-05-30 16:00 ` Bill Findlay
2003-05-30 16:17 ` Robert A Duff
2003-05-30 16:46 ` Bill Findlay
2003-05-30 16:51 ` Bill Findlay
2003-05-30 17:25 ` Stephen Leake [this message]
2003-05-30 19:40 ` Robert A Duff
2003-05-30 21:33 ` Pascal Obry
2003-05-31 16:19 ` Robert A Duff
2003-05-31 16:29 ` Robert A Duff
2003-05-30 21:38 ` Simon Wright
2003-05-30 21:51 ` chris.danx
2003-05-31 6:40 ` Pascal Obry
2003-05-31 16:21 ` Robert A Duff
2003-06-02 16:39 ` Stephen Leake
2003-05-31 16:24 ` Oliver Kellogg
2003-05-31 23:15 ` Preben Randhol
2003-06-05 5:14 ` Dave Thompson
2003-06-05 18:15 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox