Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Q: Association rules ?
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

Report this thread to moderator Post Follow-up to this message
Old Post
Oskar Enoksson
08-18-07 01:06 PM


Re: Q: Association rules ?
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
robert.corbett@sun.com
08-20-07 09:39 AM


Re: Q: Association rules ?
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
 
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Oskar Enoksson
08-21-07 12:10 AM


Re: Q: Association rules ?
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
robert.corbett@sun.com
08-21-07 09:33 AM


Re: Q: Association rules ?
Bob:

> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Jan Vorbrüggen
08-21-07 09:33 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:40 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.