vonkarman writes:
> When I tried to use a function with a pointer result, I found the status
> of the returned pointer was unassociated.
Mike showed that the problem with your code was the assignment
statement (you need to use pointer assignment) rather than anything
directly related to the use of functions. Let me note two things
First, mostly as an aside, you had asked about the text in f95:
> 14.6.2.1.2 Events that cause pointers to become disassociated
> A pointer becomes disassociated as follows:
> ..
> (4) The pointer is an ultimate component of an object of a type for which
> default initialization is specified for the component and
> (a) a function with this object as its result is invoked,
This doesn't apply to you because you aren't dealing with a type that
has default initialization. None of the intrinsic types have default
initialization - it is relevant only to derived types. Basically,
default initialization for a type initializes components of that type
whenever an object is created. Your pointer isn't a component of a
derived type at all, much less a component with default
initialization. If it were, then the above words would say that the
pointer would be nullified when the function was invoked...but that
means at the begining of the function, not at the end. It doesn't
apply to your case for the multiple reasons cited above, but that's
(in very short) what it is about.
More to the point, let me make the suggestion that it is usually a
bad idea to use functions that return pointers. It is very
error-prone. In particular, it is highly likely to cause memory
leaks. Much of the point of functions is that you can use one as
part of an expression. But for functions that return pointers,
using them in expressions is asking for memory leaks and isn't
making use of the pointer property anyway.
If the function can never be safely used in any form other than
ptr => fun(n)
then you aren't getting much benefit out of making it a function.
Might as well make it a subroutine.
There are lots of ways to make mistakes with pointers. They are a
bit error-prone in general. But using pointer functions makes the
problem worse.
--
Richard Maine | Good judgment comes from experience;
[log in to unmask] | experience comes from bad judgment.
| -- Mark Twain
|