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, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c6b9934af04bce01 X-Google-Attributes: gid103376,public From: David C. Hoos, Sr. Subject: Re: A question to the "access-type lifetime rule" Date: 2000/04/03 Message-ID: <8cail1$c33$1@nnrp1.deja.com>#1/1 X-Deja-AN: 606122318 References: <38E89AB9.7D33E88C@hamilton.physik.uni-konstanz.de> To: bernd.rinn@uni-konstanz.de X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt) X-Http-Proxy: 1.1 x21.deja.com:80 (Squid/1.1.22) for client 205.149.60.17 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Mon Apr 03 16:59:23 2000 GMT X-MyDeja-Info: XMYDJUIDdhoossr Newsgroups: comp.lang.ada Date: 2000-04-03T00:00:00+00:00 List-Id: In article <38E89AB9.7D33E88C@hamilton.physik.uni-konstanz.de>, bernd.rinn@uni-konstanz.de wrote: > Hi, > > I am new to Ada and I am looking for an elegant way to get around a > restriction posed by the "access-type lifetime rule" for > access-to-subprogram types. First of all: does anyone know, why the > access-type lifetime matters and not the access-variable lifetime? (This > should prevent dangling pointers as well, I guess.) > > I want to write an numerical integration function, that is defined in a > separate package as > > type Integrand_Type is access function ( X : Long_Float ) return > Long_Float; > function Integrate(Integrand : Integrand_Type; Lower_Bound, Upper_Bound > : Long_Float) return Long_Float; > > Now I would like to call this from a main procedure and pass an access > attribute of a nested function of the main procedure as `Integrand'. Is > there a way to reach this? (Unchecked_Access is not an allowed attribute > for subprograms and I do not want to generate a package of it's own for > the integrand function.) > > Thank you for your help. > > Best regards, > > Bernd > The fundamental problem here is that the compiler has no way of knowing that your main procedure is the "main" procedure -- i.e. that your main procedure will not return before the program ends -- and that therefore your integrand function could have gone out of scope while a pointer to it is still retained by your numerical integration package. I believe the best way to deal with this is to make the integrand function itself a library-level function -- i.e.., not encapsulated in a package. It will therefore have the same lifetime as the program, and the compiler will allow the 'Access attribute to be used anywhere in the program. A non-portable alternative (if you're using GNAT) is to use the 'Unrestricted_Access attribute -- which is jus that -- unrestricted. Sent via Deja.com http://www.deja.com/ Before you buy.