comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: access / freeing memory
Date: Mon, 15 Jul 2002 22:22:51 GMT
Date: 2002-07-15T22:22:51+00:00	[thread overview]
Message-ID: <%WHY8.1173$tt5.52024504@newssvr13.news.prodigy.com> (raw)
In-Reply-To: pan.2002.07.15.23.51.51.725321.2066@gmx.net

> head -> 1 -> 2 -> 3 -> null
>
>  head := null; (to delete all elements)
> or
>  head.next.next := null; (to delete the 3rd element)
>
> This would be a very bad programming technique in Pascal, because this
  Yes, you could do either of those assignment statements and yes you
would have a "memory leak" where the tail of the list would still be
occupying memory but would be inaccessible.  OTOH,
  This_List := head;
  ... various uses of This_List
  This_List := null;
would be OK since "head" would still be around to access the list,
and would probably be a good idea since it would prevent someone
fiddling with your list via This_List.  For instance if you have
changed to head->0->1->... you probably don't want someone going
directly to "1" via a left over This_List.

> The tutorial explains a similar procedure, called Unchecked_Deallocation,
  Given head->1->2... if you did an unchecked deallocation on "head"
it would free the memory occupied by "1" and also set "head" to null;
The 2->3->... part of the list would be allocated, but inaccessible.
What you need to do is a series of unchecked deallocations, say

  procedure Free is new Ada.Unchecked_Deallocation(...);

  procedure Free_List(x : List_Pointer) is
  begin
    if x.next /= null then Free_List(x.next);end if;
    Free(x);
  end Free_List;
  ...
  Free_List(head);

> but it's said that it's not recommended to use it (because some other
  Unchecked_Deallocation has that name because it's not checked.  Thus
you can really screw up, so it should be used carefully.  Some systems
have automatic garbage collection, but most Ada systems don't (problems
with real-time timing surprises, for instance).  You can get some
automatic deallocation going for you with careful placement of
"type ...  is access ..."  and especially with Storage_Pools.  But
mostly, with variable size array declarations and functions that can
return variable size things, you find much less need for access types
in Ada.  You can usually do stack, rather than heap, allocation which
buys automatic, safe, deallocation, and of course speed.



  reply	other threads:[~2002-07-15 22:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-16  0:52 access / freeing memory Jan Prazak
2002-07-15 22:22 ` tmoran [this message]
2002-07-16 13:54   ` Jan Prazak
2002-07-16 11:51     ` Fabien Garcia
2002-07-16 22:59       ` Jan Prazak
2002-07-16 15:42     ` Darren New
2002-07-16 22:59       ` Jan Prazak
2002-07-17  5:22         ` Simon Wright
2002-07-17 21:36           ` Jan Prazak
2002-07-24  0:25       ` David Thompson
2002-07-23  6:15   ` Kevin Cline
2002-07-18 18:22 ` chris.danx
2002-07-19 13:32   ` Jan Prazak
2002-07-19 23:50     ` chris.danx
     [not found] <mailman.1026804243.16337.comp.lang.ada@ada.eu.org>
2002-07-19  1:42 ` Robert A Duff
replies disabled

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