----- Original Message -----
From: "Van Snyder" <[log in to unmask]>
To: <[log in to unmask]>
Sent: Sunday, November 16, 2008 6:17 AM
Subject: Re: How to check the availability of a pointer object
> Hoang Trong Minh Tuan wrote:
>> Can anyone help me,
>> I have a function with one argument whose datatype is an array pointer
>>
>> SUBROUTINE FooFunc (arg1)
>> REAL, DIMENSION(:), POINTER :: arg1
>> ...
>> END SUBROUTINE
>>
>> The memory location for arg1 can be allocated before calling the
>> subroutine or not. So, I, before doing anything with arg1, want to check
>> whether the memory space had been allocated for arg1 or not. If not, I
>> will call allocate() inside the subroutine. Anyone can tell me which
>> statement to accomplish this task?
>
> Pointers in Fortran have three states: Associated with a target, not
> associated with a target, and undefined association status.
>
> If you are careful that all of your pointers, and in particular the actual
> arguments that become associated with the ARG1 dummy argument, are always
> either associated with a target, or always nullified, then you can use the
> ASSOCIATED intrinsic to do what you want to do.
>
> In Fortran 90, you need to use the NULLIFY statement or a pointer
> assignment statement with a null target (perhaps gotten by reference to
> the NULL() intrinsic) to cause a pointer to be nullified (as opposed to
> undefined). In Fortran 95, one can provide initialization (including
> default initialization of components) using the "=> NULL()" form.
>
> There is a subclause in clause 14 of the Fortran 95 standard (clause 16 of
> the Fortran 2003 standard) that specifies the conditions under which the
> association status of a pointer becomes undefined, and attention should be
> paid to those conditions.
>
> Using Fortran 95 plus Technical Report 15581, you can use ALLOCATABLE
> instead of POINTER for ARG1 (and its associated actual arguments).
> Allocatable variables do not have a third (undefined allocation) status.
> Most (but not all) Fortran 95 compilers now support allocatable dummy
> arguments, function results, and structure components. Allocatable
> variables have better characteristics concerning optimization and memory
> leaks, so they should be preferred when possible.
>
One important point for this question that is really worth stating: The
subroutine requires an Explicit Interface. One easy way to arrange this is
to put FooFunc into a module, and USE the module in the calling code. If you
don't do this (or something equivalent) you'll never get it to work.
Qolin
[log in to unmask]
+44 777 182 5542
>> Thanks a lot.
>>
>> Tuan
>>
|