James Abeles writes:
> We have a code that passes pointers. I think the standard requires that
> you define an explicit interface for the called routine. Here is a code
> fragment. Should the compiler flag an error if an interface is not
> declared?
The compiler can't necessarily tell. Passing a pointer as an actual
argument does *NOT* necessarily require an explicit interface. It
is only if the *DUMMY* argument is a pointer that you need the
explicit interface. I consider it good practice to always have
explicit interfaces, but that's not the same thing as a requirement
of the standard.
Remember that, except in a few special situations, references to a
pointer are really references to its target. That's what happens when
you pass a pointer actual argument to a nonpointer dummy - it is the
target that gets passed, not really the pointer itself. Only when
the dummy is also a pointer to you have the special case that requires
an explicit interface.
> Here is abc which is compiled seperately:
>
> subroutine abc(idim,pu,pv)
> #include xyz <<jj defined here
> complex pu(idim,jj),pv(idim,jj)
>
>
> Second question, can the interface be declared :
>
> interface
> subroutine abc(idim,pu,pv)
> integer idim
> complex pu(:,:),pv(:,:)
> end interface
*NO* *NO* *NO*. This would be lying about the interface, which
will cause no end of grief. The best (in my opinion) way to get
explicit interfaces is to use module procedures. One of the good
things about that is that you get the interfaces for "free" (you
don't have to write interface bodies). Another good thing about it
is that the interfaces are actually correct.
It is *VERY* important that, if you do write an interface body,
it must correctly describe the interface of the subroutine.
Usually the compiler cannot check this for you; if it had the
information to check it, then it wouldn't really have needed the
interface body in the first place. In some situations and with some
compilers you might get checking of this, but it is not al all
something you can count on.
In your case, the actual subroutine has explicit shape dummy
arguments, but your interface body has assumed shape ones. This is a
*MAJOR* difference; explicit-shape dummies are handled completely
differently from assumed-shape ones. That difference is one of the
reasons why you need an explicit body if you have assumed shape
dummies - the compiler needs to know which of the two very different
ways to pass the argument; if the interface is not explicit, then
the compiler basically assumes explicit shape (or assumed size,
but that's basically the same mechanism as explicit shape).
If you do write an interface body, it should look *EXACTLY* like
the relevant declarations of the actual subroutine...a line-by
line copy. You can get by with some differences, but they really
should look awfully similar, far more similar than
> complex pu(idim,jj),pv(idim,jj)
vs
> complex pu(:,:),pv(:,:)
P.S. You might, however, want to consider changing the subroutine
so that it does use assumed shape. Depending on the compiler,
passing a pointer actual argument to an explicit shape dummy
might cause copy-in/copy-out, which you probably don't want.
Newer compilers are better about that, but it can still be a
problem with some compilers.
--
Richard Maine | Good judgment comes from experience;
[log in to unmask] | experience comes from bad judgment.
| -- Mark Twain
|