Reputation: 3267
I'm unexperienced in Fortran. I'm trying to declare a memory pointer in a file named 'common', then allocate memory for that pointer in a file named "main.f", then call a subroutine from another file named "PrintArray.f". The whole thing seg faults in "PrintArray.f". How can I correct this?
This is the file named 'common':
REAL, DIMENSION (:), POINTER :: p_dynamicArray
This is the first Fortran file (main.f):
PROGRAM Main
include 'common'
INTEGER :: ArraySize, i
ArraySize = 4
ALLOCATE (p_dynamicArray(ArraySize))
DO i = 1, ArraySize
p_dynamicArray(i) = i
END DO
call PrintArray()
DEALLOCATE (p_dynamicArray)
END
This is the second Fortran file (PrintArray.f):
subroutine PrintArray()
include 'common'
INTEGER :: i
DO i = 1, 4
WRITE(0,*) "PrintArray.f: ", p_dynamicArray(i)
END DO
end
Upvotes: 0
Views: 177
Reputation: 6480
"include" has never been part of the Fortran standard (WHOOPS - SORRY, THAT'S WRONG!) - but all that it would do is simply place the contents of the file into the source code at that place.
You are NOT using common blocks, so nothing is shared between program units - PrintArray would have a completely separate pointer variable to that in main, one that hadn't been allocated or set.
If you use modules to share data then you can do this:
MODULE modcommon
REAL, DIMENSION (:), POINTER :: p_dynamicArray
END MODULE modcommon
PROGRAM Main
USE modcommon ! Allows shared content
implicit none
INTEGER :: ArraySize, i
ArraySize = 4
ALLOCATE (p_dynamicArray(ArraySize))
DO i = 1, ArraySize
p_dynamicArray(i) = i
END DO
CALL PrintArray()
DEALLOCATE (p_dynamicArray)
END PROGRAM Main
SUBROUTINE PrintArray()
USE modcommon ! Allows shared content
implicit none
INTEGER :: i
DO i = 1, 4
WRITE(*,*) "PrintArray.f: ", p_dynamicArray(i)
END DO
END SUBROUTINE PrintArray
You can break that up into source (.f90) files. Make sure the one containing the module is compiled first.
Upvotes: 1