comp.lang.ada
 help / color / mirror / Atom feed
* Strange side-effect of unchecked_deallocation
@ 1998-01-28  0:00 Andreas.Schulz
  1998-01-28  0:00 ` Stephen Leake
  0 siblings, 1 reply; 2+ messages in thread
From: Andreas.Schulz @ 1998-01-28  0:00 UTC (permalink / raw)



Boiled down to the essential, I have the following code :

type STRING_POINTER is access STRING;
DUMMY : STRING_POINTER := new STRING'("test");

procedure DEALLOCATE is new UNCHECKED_DEALLOCATION
  (STRING, STRING_POINTER);

function DEREF(X: in STRING_POINTER) return STRING is
begin
  return X.all;
end V;

procedure REPLACE ( BY_STRING : in STRING ) is
begin

  DUMMY := new STRING'(BY_STRING);
end;

So, REPLACE is intended to replace the current string of DUMMY by
a new string, like  REPLACE("new string");

However, things get strange when calling REPLACE(DEREF(DUMMY)).
In this case, DEALLOCATE(DUMMY) also messes up BY_STRING (resulting
in some strange empty array bounds range like (1880204..19) ).

Of course, the in the real code the fact that BY_STRING may in fact
be DUMMY.all is not visible where REPLACE is implemented, as well as
the fact that REPLACE changes DUMMY is invisible to the caller of
REPLACE, since the equivalent of DEREF only occasionally returns
DUMMY.all.

Now who is to blame, the compiler (VADSself Ada for Sun SPARC,
 Version 6.2.3(c)) or the designer(s) ?

Andreas

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet




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

* Re: Strange side-effect of unchecked_deallocation
  1998-01-28  0:00 Strange side-effect of unchecked_deallocation Andreas.Schulz
@ 1998-01-28  0:00 ` Stephen Leake
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Leake @ 1998-01-28  0:00 UTC (permalink / raw)



Andreas.Schulz@ri.dasa.de wrote:
> 
> Boiled down to the essential, I have the following code :
> 
> type STRING_POINTER is access STRING;
> DUMMY : STRING_POINTER := new STRING'("test");
> 
> procedure DEALLOCATE is new UNCHECKED_DEALLOCATION
>   (STRING, STRING_POINTER);
> 
> function DEREF(X: in STRING_POINTER) return STRING is
> begin
>   return X.all;
> end V;
> 
> procedure REPLACE ( BY_STRING : in STRING ) is
> begin

    DEALLOCATE (DUMMY); -- you forgot this?
 
>   DUMMY := new STRING'(BY_STRING);
> end;
> 
> However, things get strange when calling REPLACE(DEREF(DUMMY)).
> In this case, DEALLOCATE(DUMMY) also messes up BY_STRING (resulting
> in some strange empty array bounds range like (1880204..19) ).

That's why it's called Unchecked_Deallocation, instead of
Nice_Safe_Deallocation :)
 
> Now who is to blame, the compiler (VADSself Ada for Sun SPARC,
>  Version 6.2.3(c)) or the designer(s) ?

not the compiler :)

Actually, the behavior in this case is implementation defined, so
another compiler might do something different (for example, if it passes
By_String by copy instead of reference). But it isn't safe, and it isn't
portable.

You can add reference counting; DEREF should indicate that DUMMY is in
use, so REPLACE won't deallocate it. You need a limited controlled type
to do this right.

-- 
- Stephe




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

end of thread, other threads:[~1998-01-28  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-28  0:00 Strange side-effect of unchecked_deallocation Andreas.Schulz
1998-01-28  0:00 ` Stephen Leake

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