Silverfrost Forums

Welcome to our forums

Pointer arguments

5 Jun 2009 4:17 #4669

The following program generates a run-time error at line 20, which simply assigns the value 0 to the argument i in subroutine s2. It seems to work on other compilers.

MODULE m
  IMPLICIT NONE
  TYPE t1
     REAL :: r
  END TYPE t1
  TYPE(t1), POINTER, PUBLIC :: x(:)
CONTAINS
!
 SUBROUTINE s1 (a,i)
  INTEGER :: i
  TYPE(t1), POINTER :: a(:)
  i=0
  CALL s2 (a,i)
  RETURN
 END SUBROUTINE s1
!
 SUBROUTINE s2 (a,i)
  INTEGER :: i
  TYPE(t1), POINTER :: a(:)
  i=0
  RETURN
 END SUBROUTINE s2
END MODULE m

PROGRAM p
  USE m
  IMPLICIT NONE
  INTEGER :: i
!
  CALL s1 (x,i)
END PROGRAM p
5 Jun 2009 8:45 #4670

... I should have mentioned that the program crashes only when compiled with /CHECK.

6 Jun 2009 3:05 #4671

Simon, I can't see where 'x' is defined or dimensioned. John

6 Jun 2009 1:08 #4672

You have discovered a bug in the compiler. Basically you have pushed the argument checking process to its limits and the address of i has got corrupted on returning from S2.

However, you do not need to pass x in the calls to S1 and S2 because x is declared in the same module as S1 and S2 and so will be in scope anyway. If you avoid passing x as an argument then the problem goes away.

6 Jun 2009 7:21 #4674

Good idea, but what if I want to call the same routine with two different arrays?

MODULE m
  IMPLICIT NONE
  TYPE t1
     REAL :: r
  END TYPE t1
  TYPE(t1), POINTER, PUBLIC :: x(:)
  TYPE(t1), POINTER, PUBLIC :: y(:)
CONTAINS
!
 SUBROUTINE s1 (a,i)
  INTEGER :: i
  TYPE(t1), POINTER :: a(:)
  i=0
  CALL s2 (a,i)
  RETURN
 END SUBROUTINE s1
!
 SUBROUTINE s2 (a,i)
  INTEGER :: i
  TYPE(t1), POINTER :: a(:)
  i=0
  RETURN
 END SUBROUTINE s2
END MODULE m

PROGRAM p
  USE m
  IMPLICIT NONE
  INTEGER :: i
!
  CALL s1 (x,i)
  CALL s1 (y,i)
END PROGRAM p 
7 Jun 2009 7:12 #4675

For the moment you will need to find a different way to code this or avoid using /check etc. Hopefully the bug will be fixed for the next release.

It will probably work if you reverse the arguments because it is the first that is corrupting the second.

15 Jun 2009 9:29 #4699

This bug has now been fixed for the next release.

Please login to reply.