deallocating via pointer associated with allocatable array

From: Bart Vandewoestyne (MyFirstName.MyLastName_at_telenet.be)
Date: 03/23/05


Date: Wed, 23 Mar 2005 16:26:30 +0000 (UTC)

Section 6.5.3 in Fortran 95/2003 explained:

"Its general form is

deallocate(allocate-object-list [,stat=stat])

where each allocate-object is an allocatable array that is allocated or
a pointer that is associated with the whole of a target that was
allocated through a pointer in an allocate statement \footnote{Note that
this excludes a pointer that is associated with an allocatable array}"

Does this mean the deallocate statement at the end of the following program
falls under this exception in the footnote and should therefore give compiler
errors/warnings or runtime errors or something alike?

program test_deallocate

  implicit none

  ! The allocatable array we are gonna point to
  integer, dimension(:), allocatable, target :: the_target

  ! The pointer that is gonna point to our allocatable array
  integer, dimension(:), pointer :: p

  ! Allocate memory for our allocatable array
  allocate(the_target(10))

  ! Now associate the pointer with this allocatable array
  p => the_target

  ! Deallocating through the pointer seems to work... ???
  deallocate(p)

end program test_deallocate

I am asking this because somewhere in my bigger code i had something like:

      real(kind=dp), dimension(:), allocatable, target :: temp

and in the code i had a pointer pointing to this allocatable, and then I tried
to deallocate the memory through a deallocate of the pointer. When i ran my
code, i got:

Pointer in DEALLOCATE is ASSOCIATED with an ALLOCATABLE array
Program terminated by fatal error
Aborted

Changing the declaration of temp into:

      real(kind=dp), dimension(:), pointer :: temp

solves my problem, but of course I wanted to understand exactly why, and i
tried to write a simple testcase which suffers from the same error
(see the above program), but apparently there still must be something
wrong as when I try to run my testprogram i cannot reproduce the error as above.
I don't see what is wrong with my simple testcase, since i am trying to
deallocate through a pointer which is associated with an allocatable array,
isn't it? Then why don't i get the same runtime error as cited above?

Regards,
Bart

PS: I am compiling with
Fortran Company/NAG F compiler Release 20020224

-- 
	"Share what you know.  Learn what you don't."


Relevant Pages

  • Re: allocatable and pointer in a type
    ... allocatable or pointer parameters use memory. ... they are not allocate! ... Thus, in order to find out exactly how it reports the use memory, you will need to read your vendor documentation. ... Third, allocatable arrays as components of derived types, a Fortran 2003 feature, occupy memory regardless of whether the allocatable array is actually allocated. ...
    (comp.lang.fortran)
  • Re: ALLOCATABLE or POINTER
    ... pointer to an array or an allocatable array? ... I think that any Fortran programmer understands why it is ... possible to replace a dummy normal argument either by a actual pointer ...
    (comp.lang.fortran)
  • Re: Fortran and .NET (C#)
    ... You *CANNOT* leak memory because of a user error with Fortran allocatable arrays. ... That is an intentional part of the design of Fortran allocatables. ... Namely, the language does not permit more than one "pointer" (by which I mean allocatable array variable, not a real pointer) to refer to a given piece of memory allocated as an allocatable array, and therefore, when a "pointer" is dereferenced, the compiler can assume the reference count has gone to zero and thus deallocate the memory. ...
    (comp.lang.fortran)
  • Re: ALLOCATABLE or POINTER
    ... pointer needs to be associated to an allocatable array. ... run-time error should issue: here this is simply a programmer ... Imagine passing a pointer that points to a slice of a static array to an allocatable dummy argument with INTENT. ...
    (comp.lang.fortran)
  • A question about deallocate() and others in Fortran
    ... I am a beginer in Fortran, So I want to know the ... What is the difference between the deallocate() and nullifyfunction ... Before I want to deallocate a pointer varibale, ...
    (comp.lang.fortran)