comp.lang.ada
 help / color / mirror / Atom feed
From: "Alex R. Mosteo" <devnull@mailinator.com>
Subject: Re: Unchecked deallocation issues
Date: Fri, 09 Dec 2005 11:06:47 +0100
Date: 2005-12-09T11:06:47+01:00	[thread overview]
Message-ID: <43995737.6060109@mailinator.com> (raw)
In-Reply-To: <1134120125.299242.138610@g44g2000cwa.googlegroups.com>

ejijott@gmail.com wrote:
> Mhmm, hmm. Tried using just Free(Node) and indeed Node goes null..
> which puzzles me even more.. the litterature im using states that
> (freely translated from swedish):
> 
> "... using this technique to return dynamically allocated memory you,
> yourself, is responsible for making sure that there will exist no
> pointers to memory with undefined content".
> 
> In my binary tree, if I call Free() on, for example, an leaf .. would
> the parents left/right point to "memory with undefined content"? And if
> so, how would I go about to make it not point to that? Im guessing that
> Free(parent.left) would be the same as calling Free(Node) since they
> are both of type Storage? Or have I completly misunderstood the use of
> the pointers?

Unchecked_Deallocation will nullify just the pointer passed to it. I.e., 
aliased pointers will not be accordingly nullified. It's plain and old 
allocate/free strategy.

declare
    type String_Access is access all String;
    procedure Free is new Ada.Unchecked_Deallocation
      (String, String_Access);
    A : String_Access := new String'("Hello");
    B : String_Access := A;
begin
    Free (A);
    --  Now A is null but B now points to deallocated space.
end; -- Disclaimer: not compiled.

> What would an concrete example be of not making sure that there exists
> no pointers to memory with undefined content? (I.e. screwing it up :) )

You can do it the old way; i.e. by hand. In that respect the package 
Gnat.Debug_Pools can be very handy to find problems.

I think there was an article about "Smart_Pointers" which specifically 
implement a pointer abstraction where freeing a pointer automatically 
nullified every aliased pointer to the same data.

[Sorry, I just can't find it now. It usually came near the top in google 
when looking for ada smart pointer but no more it seems].


(Not to be confused with some other smart pointers where the smart bit 
refers to automatic deallocation when references go to zero, a la Java. 
That would be, for example:

http://www.adapower.com/index.php?Command=Class&ClassID=Patterns&CID=276

)



  reply	other threads:[~2005-12-09 10:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-09  5:15 Unchecked deallocation issues ejijott
2005-12-09  6:40 ` Jeffrey R. Carter
2005-12-09  9:22   ` ejijott
2005-12-09 10:06     ` Alex R. Mosteo [this message]
2005-12-09 10:18     ` Niklas Holsti
2005-12-09  6:44 ` Jeffrey R. Carter
replies disabled

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