Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointer initialization in a pure procedure

38 views
Skip to first unread message

m

unread,
Nov 11, 2009, 4:52:03 AM11/11/09
to
Dear all,
ifort compiles the attached code without complains (even with
-check all -stand f03 -warn all) while gfortran and g95 refuse it
because...

In file mod_xyz.f90:9

type(ilist), pointer :: next => null()
1
Error: Initialization of pointer at (1) is not allowed in a PURE
procedure

Two questions:
1) who is right?
2) if this is forbidden, why is it so? Or in other words, what is the
difference between a type definition in a subroutine and one placed
before the CONTAINS (apart from the scope)?

Thank you!
Marco


module mod_xyz

implicit none

contains

pure subroutine psub()
type ilist
type(ilist), pointer :: next => null()
integer :: i
end type ilist
end subroutine psub

end module mod_xyz

Arjen Markus

unread,
Nov 11, 2009, 5:16:14 AM11/11/09
to

Pure functions and subroutines should not have side effects. An
initialisation
like the above means that there is a side effect:

if ( .not. associated(next) ) then
! Only run the first time the routine is called
allocate( next )
...
endif
...

This means the routine is doing something different the first time it
is called and that is in conflict with the Fortran definition of
"pure".

I would say that gfortran and g95 are right to complain, but the error
message is probably not required by the standard. The somewhat lame
argument is that your program is non-conformant, so the compiler may
do what it pleases ...

Regards,

Arjen

m

unread,
Nov 11, 2009, 5:29:33 AM11/11/09
to

Arjen,
thank you, I see your argument for a local variable:

pure subroutine psub()
type(xyz), pointer :: next => null()
end subroutine psub

but in my example the initialization is part of a type definition, so
it doesn't require a special handling at the first execution (or does
it, nevertheless?). What I don't see is why this is fine:


module mod_xyz

implicit none

type ilist
type(ilist), pointer :: next => null()
integer :: i
end type ilist

contains

pure subroutine psub()
type(ilist) :: var

end subroutine psub

end module mod_xyz


but this is not


module mod_xyz

implicit none

contains

pure subroutine psub()
type ilist
type(ilist), pointer :: next => null()
integer :: i
end type ilist

type(ilist) :: var

Reinhold Bader

unread,
Nov 11, 2009, 5:36:54 AM11/11/09
to
Hello,

while there are restrictions with respect to avoiding side effects,
these apply to default initialization via a type declaration statement
(or data statement) which would imply the SAVE attribute, and
therefore constitute a side effect.
However, default initialization of type components does not, even
if you go on to declare

type(ilist) :: x

within psub (and as long as you do not default initialize x).
So I think gfortran and g95 are incorrectly refusing the code.

Apart from this: Unless you absolutely know that you're only ever going
to use ilist inside psub, it is probably a better idea to put the type definition
into the specification part of the module from the point of view
of potential reuse, even if only by other module procedures in mod_xyz.

Regards
Reinhold

m schrieb:

Arjen Markus

unread,
Nov 11, 2009, 7:15:49 AM11/11/09
to
> end module mod_xyz- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

Oops, I missed that part! Yes, Reinhold is quite probably right:
gfortran and
g95 erroneously complain here.

The solution would be to either put the definition outside the pure
routine
or to avoid the default initialisation - it should not be necessary as
now
it can only be used inside the pure routine and therefore you have
full control
over its correct use.

Regards,

Arjen

m

unread,
Nov 12, 2009, 4:07:07 AM11/12/09
to
> Oops, I missed that part! Yes, Reinhold is quite probably right:
> gfortran and
> g95 erroneously complain here.

Arjen, Reinhold, OK, thank you.

Marco

0 new messages