알 수없는 크기의 벡터와 배열을 반환하는 하위 함수를 호출하는 데 문제가 있습니다. 나는 함수를 호출하는 변수를 어떻게 declear할지 모른다. 이 프로그램은 컴파일하지만, 오류 메시지와 함께 충돌 않습니다 : 영어 메모리 세그먼트 오류 같은 것입니다Fortran : 알 수없는 크기의 벡터와 배열을 반환하는 서브 루틴 호출
Minnesegmentsfeil (덤프 코어)
.
내 주요 프로그램은 다음과 같습니다.
program transfer
! use konstanter
implicit none
! real :: f3eksitasjon
! real :: amp, w, F3a, eta3a
real, allocatable, dimension(:) :: Aw, Vp, Vtot, Ym, Zm, Zt, Ds
real, allocatable, dimension(:,:) :: Spos
integer, allocatable, dimension(:) :: Ns, Dir
integer :: i, N
call geometry(N, Aw, Vp, Vtot, Ym, Zm, Zt, Ds, Ns, Spos, DIR)
print *, N, Aw(1), Vp(1), Vtot(1), Ym(1), Zm(1), Ds(1), Ns(1), Spos(1,1), Spos(1,2), Dir(1)
end program transfer
서브 루틴 지오메트리는 파일에서 읽습니다. 벡터의 길이와 배열의 크기는 읽는 파일에 따라 다릅니다.
subroutine geometry(N, Aw, Vp, Vtot, Ym, Zm, Zt, Ds, Ns, Spos, DIR)
implicit none
integer, intent(out) :: N
real, dimension(:), allocatable, intent(out):: Aw, Vp, Vtot, Ym, Zm, Zt, Ds
real, dimension(:,:), allocatable, intent(out) :: Spos
integer, dimension(:),allocatable, intent(out) :: Ns, DIR
real, dimension(:), allocatable :: Bp, Hp, Lp, Vs
real, parameter :: pi = 4.0*atan(1.0)
integer :: i
open(unit = 11, file = "semisub.dat")
read(11,*) N
allocate(Aw(N))
allocate(Vp(N))
allocate(Vtot(N))
allocate(Ym(N))
allocate(Zm(N))
allocate(Zt(N))
allocate(Ds(N))
allocate(Spos(N,4))
allocate(Ns(N))
allocate(DIR(N))
allocate(Hp(N))
do i = 1,N
read(11,*) Bp(i), Hp(i), Lp(i), Ym(i), Zm(i), DIR(i)
read(11,*) Ns(i), Ds(i)
If (Ns(i) > 8) STOP "Feil i semisub.dat. For mange sOyler"
read(11,*) Spos(i,1:Ns(i))
end do
Zt(:) = Zm(:) + Hp(:)/2
Aw(:) = 2 * Ds(:) * Ns(:)
Vp(:) = 2 * Lp(:) * Bp(:) * Hp(:)
Vs(:) = 2 * Ns(:) * pi * (Ds/2)**2 * (-Zt(:))
Vtot(:) = Vp(:) + Vs(:)
end subroutine geometry
다음은 IanH에서 설명한대로 주 프로그램이 서브 루틴을 호출하는 방법을 "알 수 있도록"코드를 정렬하는 방법의 예입니다. http://stackoverflow.com/questions/6511711/computing-the-cross -product-of-two-vectors-fortran-90 –
메인 프로그램에서 서브 루틴이 반환하는 차원을 알지 못하기 때문에 여전히 치수 (:)로 할당하는 것이 맞습니까? – SM411
예. 컴파일 할 때 배열 차원을 알 수 없다면,'allocatable' 속성으로 선언하십시오. 할당은 서브 루틴에서 수행 될 수 있지만 주 루틴은 차원이 "가변적"임을 알아야합니다. –