For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2004 > Pointer function usage









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Pointer function usage
Madhusudan Singh

2004-07-18, 8:56 am

Hi

I was wondering if someone can point me to some example of usage of pointer
valued functions.

Currently, I am working with the following code :

module prog_types
type :: fourints
integer(i4b) :: kn1
integer(i4b) :: kn2
integer(i4b) :: k1
integer(i4b) :: k2
end type fourints
end module prog_types

module global
use prog_types
type(fourints), target :: qkrecs
type(fourints), pointer :: ptr_indices
end module global

module utilfunctions
function getindices(q1,q2) result(ptr_fourints)
use prog_types
real(dp), intent(in) :: q1,q2
type(fourints), pointer :: ptr_fourints

... statements that assign values to ptr_fourints%<fields> ...

end function getindices
end module utilfunctions

subroutine initialize
use prog_types
use global

nullify(ptr_indices)
if(.not.associated(ptr_indices,qkrecs)) then
allocate(ptr_indices,STAT=istat)
if(istat.ne.0) then
print*, 'Failed to allocate pointer'
stop
end if
ptr_indices=>qkrecs
end if

end subroutine initialize

subroutine calcindices
use prog_types
use global
use utilsfunctions, only : getindices

ptr_indices=>getindices(q1,q2)
end subroutine calcindices

When I compile this, I get the following error :

Error 294 : Function GETINDICES has not been given a result value


I do not know what am I doing wrong. Should I include an explicit interface
to the pointer valued function in calcindices (then there would be a
multiple definition due to the use statement) ?

I guess I could always declare 4 integers in global and put an end to this
problem, but I am curious about what I am doing wrong.

Any suggestions / pointers(!) to examples etc. would be welcome.

Thanks.
Michael Metcalf

2004-07-18, 8:56 am


"Madhusudan Singh" <spammers-go-here@yahoo.com> wrote in message
news:2luploFgpjn2U1@uni-berlin.de...
>
> Error 294 : Function GETINDICES has not been given a result value
>


I found different errors, the main one being that you mis-spell
utilSfunctions in the use statement. The following code executes, but I
haven't examined it further in detail (it is Sunday, after all).

Hope that helps,

Mike Metcalf

module prog_types
integer, parameter :: i4b = 4, dp = kind(0d0)
type :: fourints
integer(i4b) :: kn1
integer(i4b) :: kn2
integer(i4b) :: k1
integer(i4b) :: k2
end type fourints
end module prog_types

module global
use prog_types
type(fourints), target :: qkrecs
type(fourints), pointer :: ptr_indices
end module global

module utilfunctions
contains
function getindices(q1,q2) result(ptr_fourints)
use prog_types
real(dp), intent(in) :: q1,q2
type(fourints), pointer :: ptr_fourints

ptr_fourints%k1 = 1

end function getindices
end module utilfunctions
program main
contains
subroutine initialize
use prog_types
use global

nullify(ptr_indices)
if(.not.associated(ptr_indices,qkrecs)) then
allocate(ptr_indices,STAT=istat)
if(istat.ne.0) then
print*, 'Failed to allocate pointer'
stop
end if
ptr_indices=>qkrecs
end if

end subroutine initialize

subroutine calcindices
use prog_types
use global
use utilfunctions, only : getindices
real(dp) :: q1, q2
ptr_indices=>getindices(q1,q2)
end subroutine calcindices
end


Madhusudan Singh

2004-07-18, 3:58 pm

Michael Metcalf wrote:

>
> "Madhusudan Singh" <spammers-go-here@yahoo.com> wrote in message
> news:2luploFgpjn2U1@uni-berlin.de...
>
> I found different errors, the main one being that you mis-spell
> utilSfunctions in the use statement. The following code executes, but I
> haven't examined it further in detail (it is Sunday, after all).
>
> Hope that helps,
>
> Mike Metcalf
>


Thanks for your response.

What I wrote was a skeleton of the part of a large code that uses the
pointer function. I had neglected to define i4b, dp etc. for you. Sorry
about that.

And I am assigning values to the fields of ptr_fourints in getindices
(indicated by the dots in the "code" that I posted). Yet, I keep getting
that pesky compilation error.

I will try to fix it for some reasonable amount of time, until I come to the
conclusion that using pointer valued functions is just too much trouble :)

> module prog_types
> integer, parameter :: i4b = 4, dp = kind(0d0)
> type :: fourints
> integer(i4b) :: kn1
> integer(i4b) :: kn2
> integer(i4b) :: k1
> integer(i4b) :: k2
> end type fourints
> end module prog_types
>
> module global
> use prog_types
> type(fourints), target :: qkrecs
> type(fourints), pointer :: ptr_indices
> end module global
>
> module utilfunctions
> contains
> function getindices(q1,q2) result(ptr_fourints)
> use prog_types
> real(dp), intent(in) :: q1,q2
> type(fourints), pointer :: ptr_fourints
>
> ptr_fourints%k1 = 1
>
> end function getindices
> end module utilfunctions
> program main
> contains
> subroutine initialize
> use prog_types
> use global
>
> nullify(ptr_indices)
> if(.not.associated(ptr_indices,qkrecs)) then
> allocate(ptr_indices,STAT=istat)
> if(istat.ne.0) then
> print*, 'Failed to allocate
> pointer' stop
> end if
> ptr_indices=>qkrecs
> end if
>
> end subroutine initialize
>
> subroutine calcindices
> use prog_types
> use global
> use utilfunctions, only : getindices
> real(dp) :: q1, q2
> ptr_indices=>getindices(q1,q2)
> end subroutine calcindices
> end


--
DISOBEDIENCE, n. The silver lining to the cloud of servitude.

--- from The Devil's Dictionary, Ambrose Bierce

James Van Buskirk

2004-07-18, 3:58 pm

"Madhusudan Singh" <spammers-go-here@yahoo.com> wrote in message
news:2luploFgpjn2U1@uni-berlin.de...

> Error 294 : Function GETINDICES has not been given a result value


You give to indication in your snippet of where you point
the result variable (PTR_FOURINTS) at something, whether by
an ALLOCATE statement or POINTER assignment (with => ).

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


Madhusudan Singh

2004-07-18, 3:58 pm

Madhusudan Singh wrote:

> Michael Metcalf wrote:
>
>
> Thanks for your response.
>
> What I wrote was a skeleton of the part of a large code that uses the
> pointer function. I had neglected to define i4b, dp etc. for you. Sorry
> about that.
>
> And I am assigning values to the fields of ptr_fourints in getindices
> (indicated by the dots in the "code" that I posted). Yet, I keep getting
> that pesky compilation error.
>
> I will try to fix it for some reasonable amount of time, until I come to
> the conclusion that using pointer valued functions is just too much
> trouble :)
>
>


Some wierd info :

The code that I was having so much trouble compiling :

1. Croaks on Intel IFC 7.1
2. Compiles on Intel IFC 8.0
3. Compiles on Sun f90.

Michael Metcalf

2004-07-19, 3:57 am


"Madhusudan Singh" <spammers-go-here@yahoo.com> wrote in message
news:2lvqf2Fhg36dU1@uni-berlin.de...
>
> Some wierd info :
>
> The code that I was having so much trouble compiling :
>
> 1. Croaks on Intel IFC 7.1
> 2. Compiles on Intel IFC 8.0
> 3. Compiles on Sun f90.
>


That's not weird - just shows Intel has a bug that should be reported.

Regards,

Mike Metcalf


Jan Vorbrüggen

2004-07-19, 8:56 am

>> Error 294 : Function GETINDICES has not been given a result value
> You give to indication in your snippet of where you point
> the result variable (PTR_FOURINTS) at something, whether by
> an ALLOCATE statement or POINTER assignment (with => ).


That's what I also think is the problem. To clarify, a RESULT of a
function by necessity is INTENT(OUT), which means it is undefined on
entry into the function.

Given this, compilers accepting this code have a bug.

Jan
Richard Maine

2004-07-19, 4:00 pm

Madhusudan Singh <spammers-go-here@yahoo.com> writes:

> And I am assigning values to the fields of ptr_fourints in getindices...


The fields don't matter. What you are returning is the pointer.
It is perfectly valid for a pointer to point to something that has
undefined fields. Those undefined fields might later prove to be
a problem, but that would be later when you tried to reference the
fields. The function call itself would be perfectly valid.

As James and Jan mentioned, I see no evidence that you ever define
the pointer. If so, that's almost certainly the problem. This doesn't
really have anything to do with functions. That's a common problem
with basic pointer use - that you need to point it at something before
you can reference the components.

But the main reason I'm posting is a different point, since I think
James and Jan adequately covered that one.

> until I come to the
> conclusion that using pointer valued functions is just too much trouble :)


I would advocate for that position. Not that one can't get pointer-valued
functions to work. The standard does define them and they ought to work.
I'm suspicious that James and Jan have found the source of your problem;
or at worst, maybe there even is a compiler bug buried somewhere. But
even if you get it all working, I'd tend to argue against it.

I suggest instead using a subroutine with a pointer argument. (Intent(out)
when f2003 finally allows intent for pointer dummies, but you can't do
that yet).

To me, pointer-valued functions provide many opportunity for problems,
with negligable benefit. I consider that the main reason for functions
is to be able to use the function result in an expression. If the
only way that you are ever going to use a function is in the form

y => f(x)

then it could just as well be written as

call probably_a_different_name(x,y)

(where I acknowledge that the same name as one would choose for a
function might not fit the subroutine role as well. For example, I
tend to use noun forms for function names and verb forms for
subroutine names.)

If you use a pointer-valued function in any form other than y=>f(x),
it is just asking for memory leaks, so in a way, I view the ability
to use it in those other forms an invitation to error, as is the
ability to write

y = f(x)

which is almost certainly a mistake if y and f are pointers. (Possibly
legal depending on details, but quite unlikely to be what was intended).

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com