comp.lang.ada
 help / color / mirror / Atom feed
From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk)
Subject: Re: "free"ing allocated memory
Date: 1997/11/16
Date: 1997-11-16T00:00:00+00:00	[thread overview]
Message-ID: <879660687.78snx@jvdsys.nextjk.stuyts.nl> (raw)
In-Reply-To: 34693B2D.C53FFFC3@access.hky.com


In article <34693B2D.C53FFFC3@access.hky.com> randyk@access.hky.com writes:

>I allocate/create the new memory blocks with this routine.. But I cannot
>seem to figure out how to "free" the memory in Ada.

>      --this function creates a memory location and returns the address
>      Node : P := new Link;   --create new memory location

Ada was developed with the possibility of garbage collection. Which means
the language system itself will reclaim the memory no longer in use. Much
like Java does.

However, for several reasons (prominently embedded systems, real-time
systems and efficiency) most Ada compilers do not use it.

In this case the language offers the possibility of free-ing the memory
yourself, using Ada.Unchecked_Deallocation. As a type-safe language,
this procedure has to know what kind of memory it is suppossed to reclaim.
Therefor it is implemented as a generic procedure.

If your course has not touched on this subject before, you probably should
not be using it for an assigment.

But for fun: a generic is much like a C++ template in that it creates a
new procedure specifically for the types you supply it with.

In this case there are two significant types:

   Link - which is a record structure dynamically allocated
   P    - A access type for a Link record.

To free the allocated link you need to:

a) with the Ada.Unchecked_Deallocation package

b) Create a free procedure for your type P

Like:

   with Ada.Unchecked_Deallocation;

   procedure Free is new Ada.Unchecked_Deallocation (Object => Link,
                                                     Name   => P);

Now you can reclaim your allocated memory with

   Free (Node);

Hope this makes some sense...

--

-- Jerry van Dijk | Leiden, Holland
-- Consultant     | Team Ada
-- Ordina Finance | jdijk@acm.org




  reply	other threads:[~1997-11-16  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-11-12  0:00 "free"ing allocated memory Randy Kosarik
1997-11-16  0:00 ` Jerry van Dijk [this message]
1997-12-16  0:00   ` Pascal MALAISE
1997-11-18  0:00 ` Stephen Leake
replies disabled

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