Re: Lower index bound confusion



highegg wrote:
On May 25, 2:33 pm, Kurt <nob...@xxxxxxxxxxx> wrote:
highegg wrote:
On May 25, 9:49 am, Kurt <nob...@xxxxxxxxxxx> wrote:
I'm confused by the rules for lower array index bound. In some
situations the index bound is retained, in other it is "shifted" so that
the lower bound becomes 1.
By experiment I noticed that if A(:) and B(:) are array pointers then e.g.
ALLOCATE(B(10:20))
A => B
WRITE(*,*) LBOUND(A,1)
A => B(10:20)
WRITE(*,*) LBOUND(A,1)
... yields
10
1
Is this guaranteed by the standard or is it compiler-dependent? Also i
noticed for dummy variables the lower index bound is by default shifted
to 1, even for array pointer actual argument, unless the dummy variable
is a pointer array, then the lower bound is retained. At least on my
compiler.
Is there some golden rule or thought behind all this that I fail to see?
Yes, there is. On pointer assignment, the bounds of the pointer are
determined as if LBOUND and UBOUND were applied to the target.
Thus, pointer assignment to a whole array variable inherits its lower
bounds, whereas assigment to a subobject gets the bounds of the
subobject, and every subobject has the default lower bounds, i.e. 1.
You can find more here:
http://en.wikipedia.org/wiki/Fortran_language_features#Pointers_as_dy...
Ok, sounds like there is some rationality here after all.

Another thing I noticed is that I can "shift" an array pointer with a
simple function. Is there some kind of intrinsic that does this? If not
- why not? I would like to avoid implementing one such function for each
element type and for different array ranks ...

FUNCTION SHIFT(X,I) RESULT(Z)
INTEGER, POINTER :: Z(:)
INTEGER, INTENT(IN) :: I
INTEGER, POINTER :: X(:)

Z => SET_LB(X,LBOUND(X,1)+I)

END FUNCTION SHIFT

FUNCTION SET_LB(Y,J) RESULT(Z)
INTEGER, POINTER :: Z(:)
INTEGER, INTENT(IN) :: J
INTEGER, TARGET :: Y(J:)

Z => Y

END FUNCTION SET_LB

Yes, some time ago I discovered this trick, too, and put
it to the Wikipedia article I hyperlinked above.
It seems the only F95 way to assign a pointer to a non-ALLOCATEd
target with custom bounds.

In F2003, you can directly specify lower bounds for the pointer:
Z(LBOUND(X,1)+I:) => X

I am missing the feature in F95, too, but there is a way, even
standard-conforming, although clumsy, so I can live with it.

Thanks for responding again. I agree, nice to see it's in F2003.

But do you know if it's standard conforming to ALLOCATE an array pointer, shift the index range as above, and then DEALLOCATE the shifted array?

It seems to work with my compiler ...

.



Relevant Pages

  • Re: Lower index bound confusion
    ... noticed for dummy variables the lower index bound is by default shifted ... is a pointer array, then the lower bound is retained. ... Another thing I noticed is that I can "shift" an array pointer with a simple function. ...
    (comp.lang.fortran)
  • Re: Lower index bound confusion
    ... By experiment I noticed that if Aand Bare array pointers then e.g. ... noticed for dummy variables the lower index bound is by default shifted ... is a pointer array, then the lower bound is retained. ...
    (comp.lang.fortran)
  • Re: Lower index bound confusion
    ... noticed for dummy variables the lower index bound is by default shifted ... is a pointer array, then the lower bound is retained. ...
    (comp.lang.fortran)
  • Re: Lower index bound confusion
    ... By experiment I noticed that if Aand Bare array pointers then e.g. ... is a pointer array, then the lower bound is retained. ... subroutine mysub(b) ...
    (comp.lang.fortran)
  • Re: ISO C Binding: Pointer to a pointer of pointers: ***argv
    ... cargis not an array pointer, it is merely a scalar ... The only pointer here is carg. ...
    (comp.lang.fortran)