From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3839fc233928a180 X-Google-Attributes: gid103376,public From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk) Subject: Re: "free"ing allocated memory Date: 1997/11/16 Message-ID: <879660687.78snx@jvdsys.nextjk.stuyts.nl>#1/1 X-Deja-AN: 289837515 References: <34693B2D.C53FFFC3@access.hky.com> Distribution: world Organization: *JerryWare HQ*, Leiden, Holland Newsgroups: comp.lang.ada Date: 1997-11-16T00:00:00+00:00 List-Id: 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