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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,55a9bf0fafa82b43 X-Google-Attributes: gid103376,public From: franke@minet.uni-jena.de (Frank Ecke) Subject: Re: Very basic questions about memory allocation Date: 1999/07/30 Message-ID: #1/1 X-Deja-AN: 507121071 Content-Transfer-Encoding: 8bit References: Content-Type: text/plain; charset=iso-8859-1 Organization: Department of Computer Science, FSU Jena, Germany Mime-Version: 1.0 User-Agent: slrn/0.9.5.6 (UNIX) Reply-To: franke@minet.uni-jena.de Newsgroups: comp.lang.ada Date: 1999-07-30T00:00:00+00:00 List-Id: Upon Fri, 30 Jul 1999 22:46:56 +0200, JIMHER@yanoesladirecci�n.com spake unto us thuswise: > I'm starting to work in ADA I'm pleased to hear that. > but i've found some problems. The principal > one is that I son't know how to free memory previously allocated with: > > type t is .... > ... > v:access t; > begin > v:=new t; > end; > > > I've tried 'free(v)', 'delete v' and others, but they don't work. I use > to program in C, and I only want an equivalent to 'free(v)'. Actually, it is all there, but it's a bit, well, hidden. Ada is a strongly typed language. Therefore, access types are not just pointers, but each access type has associated with it the type it points to. Each time you create a new access type, it is different from all the previously created access types in your program (this sentence could be rewritten without using the word ``access''). As a consequence, there cannot be a single procedure to deallocate all objects---each object is of a particular type: you need a deallocation procedure for each access type in your program. You cannot use a Free-procedure intended for strings to deallocate integer objects. On the other hand, it would be impossible for the Ada language to ``have on stock'' all the Free-procedures necessary in your program. The solution to this dilemma is given by generics. Ada provides one generic procedure, called Unchecked_Deallocation, which you are supposed to instantiate with an actual access type. See ARM (Ada Reference Manual) 13.11.2. Thus, returning to your example code from above, the following solves the problem: -- T and U are different type T is ...; type U is ...; type T_Ptr is access T; type U_Ptr is access U; -- instantiate two deallocation procedures; one for T and one for U procedure Free is new Ada.Unchecked_Deallocation(T, T_Ptr); procedure Delete is new Ada.Unchecked_Deallocation(U, U_Ptr); Foo : T_Ptr; Bar : U_Ptr; begin --- create them Foo := new T; Bar := new U; -- use them ... -- kill them Free(Foo); Delete(Bar); end; Note that you can create the *illusion* of one general deallocation procedure: you could replace ``Delete'' by ``Free'' (but be sure to replace *all* occurrences of ``Delete'' by ``Free''). Mind you, it is an illusion; there are still two different Free-procedures. The secret behind this is the concept of overloading, which is revealed in ARM 8.6. I hope this helps, Frank -- Frank Ecke In a world without walls and fences, who needeth windows and gates?