comp.lang.ada
 help / color / mirror / Atom feed
* "free"ing allocated memory
@ 1997-11-12  0:00 Randy Kosarik
  1997-11-16  0:00 ` Jerry van Dijk
  1997-11-18  0:00 ` Stephen Leake
  0 siblings, 2 replies; 4+ messages in thread
From: Randy Kosarik @ 1997-11-12  0:00 UTC (permalink / raw)



(This is the 4th time I am trying to send this to the newsgroup so,
please disregard any copies if they make it here.... ISP has a
newsserver problem)

Hi,

I have a project in school that is completed with the exception of one
part. Releasing the memory that I am no longer using in
my linked lists. While the program would never use all of the memory in
the machine, I do not like the idea of making my code
any sloppier than I already do (experience should remove my wasteful
code <HOPEFULLY!>).

I have included two snippetts of my code and am currently using GNAT
3.10p/Win95 for the creation. I will have to recompile
on GNAT2.07 <?> for DOS to demonstrate the program since my professor
does not have Win95/NT compatability.

Thanks in advance,
-Randy


-----------------------------------
The structure and pointers are declared as such...

with Gnat.Io, Text_Io; use Gnat.Io;
procedure Register is
   subtype Name_String   is String (1..15);
   subtype Ssn_String    is String (1..11);
   type Student_Struct is
   record
      First_Name     : Name_String;
      Last_Name      : Name_String;
      Ssn            : Ssn_String;
   end record;
   type Link;
   type P is access Link;
   type Link is
   record
      Line           : Student_Struct;
      Next           : P;
   end record;


----------------------------------------------
I allocate/create the new memory blocks with this routine.. But I cannot

seem to figure out how to "free" the memory in Ada.

   function New_Node(Junk : Integer) return P is
      --this function creates a memory location and returns the address
      Node : P := new Link;   --create new memory location
   begin
      Node.Next := null;      --make it point to NULL
      return (Node);          --return address of pointer
   end New_Node;






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "free"ing allocated memory
  1997-11-12  0:00 "free"ing allocated memory Randy Kosarik
@ 1997-11-16  0:00 ` Jerry van Dijk
  1997-12-16  0:00   ` Pascal MALAISE
  1997-11-18  0:00 ` Stephen Leake
  1 sibling, 1 reply; 4+ messages in thread
From: Jerry van Dijk @ 1997-11-16  0:00 UTC (permalink / raw)



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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "free"ing allocated memory
  1997-11-12  0:00 "free"ing allocated memory Randy Kosarik
  1997-11-16  0:00 ` Jerry van Dijk
@ 1997-11-18  0:00 ` Stephen Leake
  1 sibling, 0 replies; 4+ messages in thread
From: Stephen Leake @ 1997-11-18  0:00 UTC (permalink / raw)



-- Randy Kosarik wrote:
> 
> (This is the 4th time I am trying to send this to the newsgroup so,
> please disregard any copies if they make it here.... ISP has a
> newsserver problem)
> 
> Hi,
> 
> I have a project in school that is completed with the exception of one
> part. Releasing the memory that I am no longer using in
> my linked lists. While the program would never use all of the memory in
> the machine, I do not like the idea of making my code
> any sloppier than I already do (experience should remove my wasteful
> code <HOPEFULLY!>).

A great attitude; you'll make a great programmer someday.

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

You need to instantiate Ada.Unchecked_Conversion; look in your text or
ask your prof for details (s/he may be postponing teaching this for a
good reason). The "Unchecked" in the name is a warning that you are
responsible for ensuring you don't "free" something that is still being
used; this is known as the "dangling pointer" problem.

- Stephe




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "free"ing allocated memory
  1997-11-16  0:00 ` Jerry van Dijk
@ 1997-12-16  0:00   ` Pascal MALAISE
  0 siblings, 0 replies; 4+ messages in thread
From: Pascal MALAISE @ 1997-12-16  0:00 UTC (permalink / raw)



Jerry van Dijk wrote:
> 
> 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.

Even if you are able to ask the OS to "free" memory, more and more OS
(UNIX) 
wont release the associated swap space anyway.
In some platforms, another approach than unchecked deallocation can be


-- Allocates and frees cells of data access,
--  using a free list to re-use free cells.
generic
  type DATA_TYPE is private;
  type DATA_ACCESS_TYPE is access DATA_TYPE;

package DYN_DATA is

  -- Allocates a new cell.
  -- The result is the access to a pre allocated area for DATA_TYPE.
  function ALLOCATE return DATA_ACCESS_TYPE;

  -- Allocates a new cell and fills it with DATA
  -- The result is the access to a pre allocated area for DATA_TYPE,
  --  storing DATA
  function ALLOCATE (DATA : DATA_TYPE) return DATA_ACCESS_TYPE;

  -- Frees a cell. DATA_ACCESS is set to null.
  procedure FREE (DATA_ACCESS : in out DATA_ACCESS_TYPE);

end DYN_DATA;

-- body available on demand

-- 
Pascal MALAISE			| E-mail:
52 Fletcher St			|  (priv) malaise@alphalink.com.au
HAWTHORN EAST    VIC    3123	|  (prof) malaise@thomson.starway.net.au
AUSTRALIA




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-12-16  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-11-12  0:00 "free"ing allocated memory Randy Kosarik
1997-11-16  0:00 ` Jerry van Dijk
1997-12-16  0:00   ` Pascal MALAISE
1997-11-18  0:00 ` Stephen Leake

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