comp.lang.ada
 help / color / mirror / Atom feed
* Access1 := new Accessed_Type; Access1 = Access2 ...?!
@ 2004-11-27 12:12 Florian Haag
  2004-11-27 13:58 ` Jeff C r e e.m
  2004-11-27 20:42 ` Jeffrey Carter
  0 siblings, 2 replies; 6+ messages in thread
From: Florian Haag @ 2004-11-27 12:12 UTC (permalink / raw)


Hi,
I'm quite new to Ada and I hope someone can explain this to me:
I've got the following code:

Parent_State is assigned some already existing Vessel_State_Type
variable, or null.

State_Access := new Vessel_State_Type;
State_Access.All.Previous_State := Parent_State;
if State_Access /= null then
  if State_Access = State_Access.All.Previous_State then
    raise MyEx;
  end if;
end if;

Now, why is MyEx raised?

Parent_State should refer to an already existent variable, whereas
(I thought) the variable State_Access points to is just created in
the beginning of that block ...?
If the above code does not what I thought it did, how can I
achieve that? ;-)

TIA
Florian Haag






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

* Re: Access1 := new Accessed_Type; Access1 = Access2 ...?!
  2004-11-27 12:12 Access1 := new Accessed_Type; Access1 = Access2 ...?! Florian Haag
@ 2004-11-27 13:58 ` Jeff C r e e.m
  2004-11-28  9:56   ` Florian Haag
  2004-11-27 20:42 ` Jeffrey Carter
  1 sibling, 1 reply; 6+ messages in thread
From: Jeff C r e e.m @ 2004-11-27 13:58 UTC (permalink / raw)



"Florian Haag" <florianhaag@yahoo.de> wrote in message 
news:30r96lF34a891U1@uni-berlin.de...
> Hi,
> I'm quite new to Ada and I hope someone can explain this to me:
> I've got the following code:
>
> Parent_State is assigned some already existing Vessel_State_Type
> variable, or null.
>
> State_Access := new Vessel_State_Type;
> State_Access.All.Previous_State := Parent_State;
> if State_Access /= null then
>  if State_Access = State_Access.All.Previous_State then
>    raise MyEx;
>  end if;
> end if;
>
> Now, why is MyEx raised?
>
> Parent_State should refer to an already existent variable, whereas
> (I thought) the variable State_Access points to is just created in
> the beginning of that block ...?
> If the above code does not what I thought it did, how can I
> achieve that? ;-)
>
> TIA
> Florian Haag
>
>
>


I can't think of any reason why myEx should be raised. Are you sure that is 
what is happening?

Also the .ALLs in you code here are optional.


The following code (with the optional .alls)


with Text_IO;

with Ada.Exceptions;
procedure Test_Ex is

type Vessel_State_Type;

type Access_Vessel_State_Type is access Vessel_State_Type;

type Vessel_State_Type is
record
Previous_State : Access_Vessel_State_Type;
end record;
Parent_State : Access_Vessel_State_Type :=
new Vessel_State_Type;
State_Access : Access_Vessel_State_Type;
MyEx : exception;
begin
State_Access := new Vessel_State_Type;
State_Access.All.Previous_State := Parent_State;
if State_Access /= null then
if State_Access = State_Access.All.Previous_State then
raise MyEx;
end if;
end if;
Text_IO.Put_Line("No problem");
exception
when ID : others =>
Text_IO.Put_Line(Ada.Exceptions.Exception_Name(ID));
end Test_Ex;

Prints
"No problem" when it runs.



So, without seeing more of your code I can not really guess what is 
happening.







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

* Re: Access1 := new Accessed_Type; Access1 = Access2 ...?!
  2004-11-27 12:12 Access1 := new Accessed_Type; Access1 = Access2 ...?! Florian Haag
  2004-11-27 13:58 ` Jeff C r e e.m
@ 2004-11-27 20:42 ` Jeffrey Carter
  1 sibling, 0 replies; 6+ messages in thread
From: Jeffrey Carter @ 2004-11-27 20:42 UTC (permalink / raw)


Florian Haag wrote:

> if State_Access /= null then

Not responding to your question, but State_Access can never be null 
here. "new" either returns a non-null access value or an exception is 
raised (usually Storage_Error). See ARM 4.8 and 13.11.

-- 
Jeff Carter
"Many times we're given rhymes that are quite unsingable."
Monty Python and the Holy Grail
57




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

* Re: Access1 := new Accessed_Type; Access1 = Access2 ...?!
  2004-11-27 13:58 ` Jeff C r e e.m
@ 2004-11-28  9:56   ` Florian Haag
  0 siblings, 0 replies; 6+ messages in thread
From: Florian Haag @ 2004-11-28  9:56 UTC (permalink / raw)


"Jeff C r e e.m" <jcreem@yahoo.com> schrieb im Newsbeitrag 
news:9K%pd.401802$wV.389499@attbi_s54...
>
[snip]
> I can't think of any reason why myEx should be raised. Are you sure that 
> is what is happening?
[snip]

Hi,
thanks for your answer.
Got it now; in some special situations, Parent_State had been
(erroneously) freed without being set to null.

Regards,
Florian Haag 





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

* Re: Access1 := new Accessed_Type; Access1 = Access2 ...?!
@ 2004-11-29  6:51 Christoph Karl Walter Grein
  2004-11-29 17:54 ` Florian Haag
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Karl Walter Grein @ 2004-11-29  6:51 UTC (permalink / raw)
  To: comp.lang.ada

> State_Access := new Vessel_State_Type;
> State_Access.All.Previous_State := Parent_State;
> if State_Access /= null then

this if statement comes too late:
If State_Access = null, then the preceding assignment to State_Access.All.Previous_State would raise an exception: Constraint_Error, because the dereference State_Access.All is impossible.

>  if State_Access = State_Access.All.Previous_State then
>    raise MyEx;
>  end if;
> end if;

__________________________________________________________
Mit WEB.DE FreePhone mit hoechster Qualitaet ab 0 Ct./Min.
weltweit telefonieren! http://freephone.web.de/?mc=021201




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

* Re: Access1 := new Accessed_Type; Access1 = Access2 ...?!
  2004-11-29  6:51 Christoph Karl Walter Grein
@ 2004-11-29 17:54 ` Florian Haag
  0 siblings, 0 replies; 6+ messages in thread
From: Florian Haag @ 2004-11-29 17:54 UTC (permalink / raw)


"Christoph Karl Walter Grein" <AdaMagica@web.de> schrieb im Newsbeitrag 
news:mailman.148.1101711116.10401.comp.lang.ada@ada-france.org...
>> State_Access := new Vessel_State_Type;
>> State_Access.All.Previous_State := Parent_State;
>> if State_Access /= null then
>
> this if statement comes too late:
> If State_Access = null, then the preceding assignment to 
> State_Access.All.Previous_State would raise an exception: 
> Constraint_Error, because the dereference State_Access.All is impossible.

This was just inserted for debugging reasons ... I wanted to check
whether State_Access was null because I couldn't believe what
was happening ;-)

Regards,
Florian Haag 





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

end of thread, other threads:[~2004-11-29 17:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-27 12:12 Access1 := new Accessed_Type; Access1 = Access2 ...?! Florian Haag
2004-11-27 13:58 ` Jeff C r e e.m
2004-11-28  9:56   ` Florian Haag
2004-11-27 20:42 ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2004-11-29  6:51 Christoph Karl Walter Grein
2004-11-29 17:54 ` Florian Haag

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox