저는 프로그래머가 아니므로 데이터를 포인터로 제공하는 모델과 인터페이스하려고합니다. 이 포인터는 데이터가 기록되기 전에 여러 서브 루틴을 통해 전달됩니다. 메모리 누수를 피할 수 있도록이 작업을 수행하는 방법을 잘 모르겠습니다.포인터를 여러 서브 루틴으로 전달하는 올바른 방법
어레이 포인터 A
이 쓰여지기 전에 여러 서브 루틴에 전달되었다고 가정 해 봅시다. 어떻게 선언, 할당 및 할당 해제를 처리합니까?
module data
implicit none
contains
subroutine s1(a)
real, pointer, intent(out) :: a(5,5)
call s2(a)
end subroutine s1
subroutine s2(a)
real, pointer, intent(out) :: a(5,5)
integer :: i
do i = 1,5
a(:,i) = 5.0
end do
end subroutine s2
end module data
Program test
use data, only : s1, s2
real, pointer, dimension(:,:) :: A => NULL()
allocate(A(5,5))
call s1(A)
write(*,*) A
deallocate(A)
end Program test
을 찾을 수 있습니다 그 포인터의. 예를 들어 [이 관련 질문] (https://stackoverflow.com/q/29737367)을 참조하십시오. 또는 소위 _explicit shape_ nature (포인터가되는 것과 충돌). – francescalus
차원을 한 번만 지정해야합니다. 서브 루틴에서 다음과 같이 선언해야합니다 : 'real, pointer, dimension (:, :), intent (out) :: a'. ''deallocate (A)'후에, 더 이상 포인터가 필요 없다면,'nullify (A)'할 수 있습니다. –