Code Comments
Programming Forum and web based access to our favorite programming groups.I feel I don't understand the rules for pointer- and dummy argument association and aliasing problems - could someone help me? If I understand right, the following program may not do what I want (?) TYPE A_T INTEGER, POINTER :: X END TYPE A_T SUBROUTINE FOO(A,X) TYPE(A_T), INTENT(INOUT) :: A INTEGER, INTENT(INOUT) :: X A%X = A%X+1 X = X-1 END SUBROUTINE FOO PROGRAM MYPROG TYPE(A_T) A ALLOCATE(A%X) A%X = 0 CALL FOO(A,A%X) WRITE(*,*) A%X ! Will it print "0" ? END PROGRAM MYPROG Will I be safe if I declare dummy argument X with attribute TARGET in FOO ? If not, what about declaring X with attribute POINTER? What about subprograms that doesn't modify X and declare X with INTENT(IN) SUBROUTINE FOO(A,X) TYPE(A_T), INTENT(INOUT) :: A INTEGER, INTENT(IN) :: X A%X = A%X+1 END SUBROUTINE FOO PROGRAM MYPROG TYPE(A_T) A ALLOCATE(A%X) A%X = 0 CALL FOO(A,A%X) WRITE(*,*) A%X ! Will it print "1" ? END PROGRAM MYPROG Thanks /Oskar
Post Follow-up to this messageOn Aug 18, 3:50 am, Oskar Enoksson <nob...@nowhere.org> wrote: > I feel I don't understand the rules for pointer- and dummy argument > association and aliasing problems - could someone help me? > > If I understand right, the following program may not do what I want (?) > > TYPE A_T > INTEGER, POINTER :: X > END TYPE A_T > > SUBROUTINE FOO(A,X) > TYPE(A_T), INTENT(INOUT) :: A > INTEGER, INTENT(INOUT) :: X > > A%X = A%X+1 > X = X-1 > > END SUBROUTINE FOO > > PROGRAM MYPROG > > TYPE(A_T) A > ALLOCATE(A%X) > A%X = 0 > > CALL FOO(A,A%X) > > WRITE(*,*) A%X ! Will it print "0" ? > > END PROGRAM MYPROG The derived type A_T must be declared within a scoping unit. In this case the scoping unit should be a module that is used in FOO and MYPROG. The program will not be standard-conforming even then. > Will I be safe if I declare dummy argument X with attribute TARGET in > FOO? If both A and X in the subroutine FOO are given the attribute TARGET, if FOO is declared in an interface block in MYPROG, and if the declaration of the derived type A_T is fixed, the program will be standard-conforming. > If not, what about declaring X with attribute POINTER? > > What about subprograms that doesn't modify X and declare X with INTENT(IN) > > SUBROUTINE FOO(A,X) > TYPE(A_T), INTENT(INOUT) :: A > INTEGER, INTENT(IN) :: X > > A%X = A%X+1 > > END SUBROUTINE FOO > > PROGRAM MYPROG > > TYPE(A_T) A > ALLOCATE(A%X) > A%X = 0 > > CALL FOO(A,A%X) > > WRITE(*,*) A%X ! Will it print "1" ? > > END PROGRAM MYPROG If you fix the declaration of the derived type A_T, the program will be standard-conforming and it should print 1. Declaring X to have the attribute INTENT(IN) in FOO has no effect. Removing the second assignment is what does the trick. Bob Corbett
Post Follow-up to this messagePROGRAM MYPROG IMPLICIT NONE TYPE A_T INTEGER, POINTER :: X END TYPE A_T TYPE(A_T) A ALLOCATE(A%X) A%X = 0 CALL FOO(A,A%X) WRITE(*,*) A%X ! Will it print "0" ? CONTAINS SUBROUTINE FOO(A,X) TYPE(A_T), INTENT(INOUT) :: A INTEGER, INTENT(INOUT) :: X A%X = A%X+1 X = X-1 END SUBROUTINE FOO END PROGRAM MYPROG > > If both A and X in the subroutine FOO are given the attribute > TARGET, if FOO is declared in an interface block in MYPROG, > and if the declaration of the derived type A_T is fixed, the > program will be standard-conforming. Thanks for responding. So adding TARGET to both dummy arguments A and X will ensure that X and A%X are associated with the same target in FOO? I'm surprised that A needs to have the TARGET attribute - why isn't it enough that the component A%X has the POINTER attribute? /Oskar
Post Follow-up to this messageOn Aug 20, 7:33 am, Oskar Enoksson <enok_tabort...@lysator.liu.se> wrote: > PROGRAM MYPROG > IMPLICIT NONE > TYPE A_T > INTEGER, POINTER :: X > END TYPE A_T > > TYPE(A_T) A > ALLOCATE(A%X) > A%X = 0 > > CALL FOO(A,A%X) > > WRITE(*,*) A%X ! Will it print "0" ? > CONTAINS > > SUBROUTINE FOO(A,X) > TYPE(A_T), INTENT(INOUT) :: A > INTEGER, INTENT(INOUT) :: X > > A%X = A%X+1 > X = X-1 > > END SUBROUTINE FOO > END PROGRAM MYPROG > > > > Thanks for responding. So adding TARGET to both dummy arguments A and X > will ensure that X and A%X are associated with the same target in FOO? > I'm surprised that A needs to have the TARGET attribute - why isn't it > enough that the component A%X has the POINTER attribute? You're right. In this case, A % X is a pointer subobject, and so the A that appears in FOO does not need to have the attribute TARGET. Bob Corbett
Post Follow-up to this messageBob: > If both A and X in the subroutine FOO are given the attribute > TARGET, if FOO is declared in an interface block in MYPROG, > and if the declaration of the derived type A_T is fixed, the > program will be standard-conforming. You're confusing me. In this case, I see A%X and X as being aliases, and bot h are modified in the subroutine. Surely that's prohibited by the standard? > If you fix the declaration of the derived type A_T, the program > will be standard-conforming and it should print 1. [...] Removing > the second assignment is what does the trick. Agreed. Jan
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.