comp.lang.ada
 help / color / mirror / Atom feed
From: ejijott@gmail.com
Subject: Unchecked deallocation issues
Date: 8 Dec 2005 21:15:21 -0800
Date: 2005-12-08T21:15:21-08:00	[thread overview]
Message-ID: <1134105321.360940.260560@o13g2000cwo.googlegroups.com> (raw)

First some code:

<snip>
  type node;
  type storage is access node;
  type node is
    record
      left:  storage;
      right: storage;
      item:  string(1..50);
      len:   integer := -1;
      count: integer := 0;
    end record;
</snip>

<snip>
        procedure case_delete( Parent : in out Storage; Node: in out
Storage; Direction:Integer ) is
            tempnode:Storage;
        begin
            if Node.left = null and Node.right = null then
                if Direction = 0 then
                    Free(Node);
                    Node:=null;
                else
                    Free(Node);
                    Node:=null;
                end if;
            elsif Node.left /= null xor Node.right /= null then
                if Node.left = null then
                    if Direction = 0 then
                        parent.left:=Node.right;
                        Free(node);
                    else
                        parent.right:=Node.right;
                        Free(node);
                    end if;
                else
                    if Direction = 0 then
                        parent.left:=Node.left;
                        Free(node);
                    else
                        parent.right:=Node.left;
                        Free(node);
                    end if;
                end if;
            else
                put_line("two children");
            end if;
        end case_delete;
</snip>

Free() is implemented as:
procedure Free is new UNCHECKED_DEALLOCATION( Node, Storage);

When I enter case_delete() parent is, obviously, the parent of the
matching node to delete and direction is wheter the matching node is on
the left or right of the parent.

Right.. code snippet above is a part of my BST Remove() handling. The
issue I'm having is using unchecked deallocation... what im figuring
here is that my Free() procedure frees the part of memory that contains
my value, but does nothing to the pointer itself. So, if I just call
Free() on an value-pointed-to and directly after print out
value-pointed-to it _could_ still print out whatever it contains, but
it could change at any time due to the fact that that part of memory is
part of the pool again? And to solve this, I should set the pointer of
the pointed-to-value to null ... is my implementation of this correct,
as in this snippet:

<snip>
                    Free(Node);
                    Node:=null;
</snip>

But, somehow, this doesn't feel right to me. With the snippet above, am
I really setting the pointer to null, or the variable of type Storage
to null?

I tried my code by deleting a leafnode and then inserting a new with
the same value and it ended up in the same position, but I fear that I
have fundamentally misunderstood the use of unchecked deallocation.. or
have I? :)

Thanks in advance!




             reply	other threads:[~2005-12-09  5:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-09  5:15 ejijott [this message]
2005-12-09  6:40 ` Unchecked deallocation issues Jeffrey R. Carter
2005-12-09  9:22   ` ejijott
2005-12-09 10:06     ` Alex R. Mosteo
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