comp.lang.ada
 help / color / mirror / Atom feed
* Re: A question to the "access-type lifetime rule"
  2000-04-03  0:00 A question to the "access-type lifetime rule" Bernd Rinn
  2000-04-03  0:00 ` Gautier
  2000-04-03  0:00 ` David
@ 2000-04-03  0:00 ` Jean-Marc Bourguet
  2000-04-03  0:00 ` Robert Dewar
  2000-04-04  0:00 ` John English
  4 siblings, 0 replies; 6+ messages in thread
From: Jean-Marc Bourguet @ 2000-04-03  0:00 UTC (permalink / raw)


In article <38E89AB9.7D33E88C@hamilton.physik.uni-konstanz.de>,
bernd.rinn@uni-konstanz.de wrote:

> 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.)

1) Make the integrate function a generic function whose parameter
is the integrand. Then instanciate the integrate function where
you want to use it.

2) Use gnat which has an attribute allowing to bypass the rule. But
be warned not to store the access value gotten longer than the
subprogram lives.

-- Jean-Marc


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: A question to the "access-type lifetime rule"
  2000-04-03  0:00 A question to the "access-type lifetime rule" Bernd Rinn
  2000-04-03  0:00 ` Gautier
@ 2000-04-03  0:00 ` David
  2000-04-03  0:00 ` Jean-Marc Bourguet
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: David @ 2000-04-03  0:00 UTC (permalink / raw)
  To: bernd.rinn

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.




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

* Re: A question to the "access-type lifetime rule"
  2000-04-03  0:00 A question to the "access-type lifetime rule" Bernd Rinn
                   ` (2 preceding siblings ...)
  2000-04-03  0:00 ` Jean-Marc Bourguet
@ 2000-04-03  0:00 ` Robert Dewar
  2000-04-04  0:00 ` John English
  4 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 2000-04-03  0:00 UTC (permalink / raw)


In article <38E89AB9.7D33E88C@hamilton.physik.uni-konstanz.de>,
  bernd.rinn@uni-konstanz.de wrote:

> I want to write an numerical integration function, that is
> defined in a separate package as

This is a classical case where the use of a generic is far
preferable from every point of view. Forget about pointers
to subprograms completely, this is not what they are for
in Ada!

Nested functions and pointers-to-functions do not go well
together. That is why C, C++, Ada and many other languages
avoid this combination.

Ada 95 allows the combination but restricts it to be safe.

GNAT+Unrestricted_Access and GNU C allow the unrestricted
combination, but this is definitely programmer-beware
territory since dangling procedure pointers are about
as horrible a disaster as one can imagine.


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: A question to the "access-type lifetime rule"
  2000-04-03  0:00 A question to the "access-type lifetime rule" Bernd Rinn
@ 2000-04-03  0:00 ` Gautier
  2000-04-03  0:00 ` David
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Gautier @ 2000-04-03  0:00 UTC (permalink / raw)


Bernd Rinn:

As other replies point out, the generic approach is the best.

  generic
  with function Integrand_Type( X : Long_Float ) return Long_Float;
  function Integrate(Lower_Bound, Upper_Bound: Long_Float) return Long_Float;

Not only for avoiding any bad surprise with pointers, robustness or Ada-style,
but also for efficiency reasons: according to the function you plug in as generic
parameter, the contents of that function will be inlined into the "Integrate"
function, plus optimised by the compiler. Reversely, each reference to a pointer
to a function will be at least an indirection slower than the generic version.

HTH
______________________________________________________
Gautier  --  http://members.xoom.com/gdemont/gsoft.htm




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

* A question to the "access-type lifetime rule"
@ 2000-04-03  0:00 Bernd Rinn
  2000-04-03  0:00 ` Gautier
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Bernd Rinn @ 2000-04-03  0:00 UTC (permalink / raw)


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

--
Bernd Rinn
Fakult�t f�r Physik
Universit�t Konstanz

Tel. 07531/88-3812,
e-mail: Bernd.Rinn@uni-konstanz.de
PGP-Fingerprint: 1F AC 31 64 FF EF A9 67  6E 0D 4C 26 0B E7 ED 5C







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

* Re: A question to the "access-type lifetime rule"
  2000-04-03  0:00 A question to the "access-type lifetime rule" Bernd Rinn
                   ` (3 preceding siblings ...)
  2000-04-03  0:00 ` Robert Dewar
@ 2000-04-04  0:00 ` John English
  4 siblings, 0 replies; 6+ messages in thread
From: John English @ 2000-04-04  0:00 UTC (permalink / raw)


Bernd Rinn 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.)

If you declare the package where Integrate is declared like this:
  generic
  package X is ...

then in the same declaration section as the nested function you say
this:
  package Inner_X is new X;

and voila, you have smuggled the declarations from X into the inner
scope (the access level of Inner_X being the level at which the
instantiation occurs).

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




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

end of thread, other threads:[~2000-04-04  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-04-03  0:00 A question to the "access-type lifetime rule" Bernd Rinn
2000-04-03  0:00 ` Gautier
2000-04-03  0:00 ` David
2000-04-03  0:00 ` Jean-Marc Bourguet
2000-04-03  0:00 ` Robert Dewar
2000-04-04  0:00 ` John English

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