comp.lang.ada
 help / color / mirror / Atom feed
From: Jan Prazak <janp9@gmx.net>
Subject: Re: access / freeing memory
Date: Tue, 16 Jul 2002 12:54:40 -0100
Date: 2002-07-16T12:54:40-01:00	[thread overview]
Message-ID: <pan.2002.07.16.12.50.51.485455.1467@gmx.net> (raw)
In-Reply-To: %WHY8.1173$tt5.52024504@newssvr13.news.prodigy.com

On Mon, 15 Jul 2002 21:22:51 -0100, tmoran wrote:

>   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,

Then I don't undertand why the tutorial doesn't mention it. It says
something like: "it's better to let the operating system manage it", but
I haven't ever heard of an operating system which can manage memory
(de)allocations (maybe Linux does it, but I have very little knowledge
of how Linux works). This "drastic" technique is also shown in a book
about programming I own (as an Ada example program, and *even* as a
Pascal version, without "Dispose" !). I have also read that access types
in Ada are more secure than pointers in other languages, but I don't see
any reason, both versions seem to be identical.

>   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.

Let me know if I did understand this correctly: if I have a "head", which
can access nodes (in a list), and I create some nodes and connect
them,for instance:

  head -> 1 -> 2 -> 3 -> null

and if I do nothing (so the program will end), the operating system (or
compiler???) does the deallocation automatically, just because "head" is
a variable. It then goes, starting at head, through the whole list and
frees the memory occupied by every node. Right? If yes, why should I use
a second head, which points at the main head?
And how can I then access the nodes? With The_List.Head.all (first node)
etc.?

> 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);

That's what I explained, it's neccessary to dispose every node in other
to delete the list. This recursive procedure is interesting, but it maybe
not good for large lists (because of memory usage).

>> 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

What is an Ada system? You mean systems for which an Ada compiler is
available?

> with real-time timing surprises, for instance).  You can get some
> automatic deallocation going for you with careful placement of "type ...
>  is access ..."

Is this the thing I have explained above?

> and especially with Storage_Pools.  But mostly, with

What is a storage pool?

> variable size array declarations and functions that can return variable
> size things, you find much less need for access types in Ada.

I use pointers only for stacks (linked lists), trees etc.

> You can
> usually do stack, rather than heap, allocation which buys automatic,
> safe, deallocation, and of course speed.

What do you mean? Why should a FIFO be faster than a LIFO? Or do
you mean something else by this.

Thanks,
Jan




  reply	other threads:[~2002-07-16 13:54 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
2002-07-16 13:54   ` Jan Prazak [this message]
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