Array and pointer shapes

Can someone explain to me why the following program is not working, and how to make it work? In the main program, I highlight the pointer, in the sub routine, I look for the shape of the array and get the wrong values.

  program test real, pointer, dimension(:,:,:) :: arr allocate(arr(3,5,7)) print *, "In test: ",shape(arr) call sub(arr) print *, "Back in test: ",shape(arr) end program test subroutine sub(arr) real, pointer, dimension(:,:,:) :: arr print *, "In sub: ",shape(arr) end subroutine 

Output:

  In test: 3 5 7 In sub: 12694064 1 3 Back in test: 3 5 7 

thanks

PS: I am using gfortran (gcc 4.4.3)

EDIT: with gfortran 4.6, this code just doesn't compile. I get an error message:

The dummy argument 'arr' of the procedure 'sub' at (1) has an attribute that requires an explicit interface for this procedure

+4
source share
1 answer

In order to use the “advanced” capabilities of Fortran 90/95/2003/2008, the interfaces of procedures (routines and functions) must be known to the caller. This is called an "explicit" interface. gfortran 4.6 told you what the problem is. The easiest way is to put your procedures in a module. Try the following:

 module mysubs implicit none contains subroutine sub(arr) real, pointer, dimension(:,:,:) :: arr print *, "In sub: ",shape(arr) end subroutine end module mysubs program test use mysubs implicit none real, pointer, dimension(:,:,:) :: arr allocate(arr(3,5,7)) print *, "In test: ",shape(arr) call sub(arr) print *, "Back in test: ",shape(arr) end program test 
+9
source

Source: https://habr.com/ru/post/1397577/


All Articles