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

Pointer points to sub's dummy argument

2 views
Skip to first unread message

Alan

unread,
Nov 14, 2001, 8:55:40 AM11/14/01
to
Hallo.
I'm interest to know if what I will describe is legal, because I'm a
little bit confused.

I want to make the pointer p (private of module test_m) to point to n
(lives in main program). I implemented the following snippet where I'm
trying to do this, but I don't know if the implementation is
completely legal (although it works).

What about variable pin?
It dies when assign_p terminates ? If yes the point to (=>) statement
is completely buggy and illegal.

AFAIK fortran is passing always arguments as reference. Thus p is
pointing to the argument and lives as far as the argument lives (n in
current example - lives as far main program lives) and this example is
legal, but I really don't know if this is completely correct.

Can anyone please make things clear? I'm confused. The following
snippet works fine, but I don't know if this is just a coincidence.

Many thanks in advance.
Selton Alan.

!----------------------------------------------
module test_m
!
implicit none
integer(4), pointer, private:: p
!
contains
!
subroutine assign_p (pin)
integer(4), target :: pin
p => pin
end subroutine assign_p
!
end module test_m
!----------------------------------------------
program test
use test_m
integer(4), target :: n
!
n = 10
call assign_p(n)
!
end program test
!----------------------------------------------

Jan Vorbrueggen

unread,
Nov 14, 2001, 9:13:48 AM11/14/01
to
I wonder what you mean by "it works", as you have not supplied any method to
access the variable pointed to by p...

As far as I understand it, your approach should be fine: As long as the module
is in scope (in your test case, while the main program is running), p will be
in scope as well. There would be no point to have module variables if that
weren't the case.

Also, please read Richard Main's recent postings on the nature of pointers...

Jan

Richard Maine

unread,
Nov 14, 2001, 10:52:37 AM11/14/01
to
Sel...@hotmail.com (Alan) writes:

> I'm interest to know if what I will describe is legal,...

I think its ok, but I get confused myself about some of the fine
points of dummy argument targets. I'd have to go study some to
gice you a "real" answer, but the off-the-top-of-my-head one is
that it looks ok.

See Jan's reply for several points. The only thing I really have
to add here is that

> AFAIK fortran is passing always arguments as reference.

That is not true. It is the most common implementation for many
cases, but it is not required by the language and is not always
true. More to the point, if you try to make conclusions about
what ought to be legal based on this, then your conclusions are
not likely to be correct. The language is specifically designed
so that multiple argument passing mechanisms are allowed in most
places. There are places where this significantly influenced
things - if you try to draw conclusions assuming that the language
was designed around pass-by-reference, you'll just get it wrong.

--
Richard Maine | Good judgment comes from experience;
email: my last name at host.domain | experience comes from bad judgment.
host: altair, domain: dfrc.nasa.gov | -- Mark Twain

0 new messages