[SOLVED] by francescalus. 배정 밀도 동적 배열에 대한 작업 코드는 다음과 같습니다Fortran90의 동적 배열에 새 요소를 추가하는 방법
module DynamicalArrays
contains
subroutine AddToList(list, element)
IMPLICIT NONE
integer :: i, isize
double precision, intent(in) :: element
double precision, dimension(:), allocatable, intent(inout) :: list
double precision, dimension(:), allocatable :: clist
if(allocated(list)) then
isize = size(list)
allocate(clist(isize+1))
do i=1,isize
clist(i) = list(i)
end do
clist(isize+1) = element
deallocate(list)
call move_alloc(clist, list)
else
allocate(list(1))
list(1) = element
end if
end subroutine AddToList
end module DynamicalArrays
배열이 될 것이다 충전 할 수있는 데모 서브 루틴, :는 별도의 파일에 모듈의 코드를 유지하기 위해 최선의 것을
subroutine UserDArrayTest()
use DynamicalArrays
integer :: i
double precision, dimension(:), allocatable :: list
double precision :: temp
temp = 0.1
do i=1,10
temp = temp+1
call AddToList(list, temp)
end do
do i=1,10
print *, i, list(i)
end do
end
주, 모듈 코드가 메인 프로그램과 서브 루틴 코드 위에있을 때 작동한다는 것을 알았습니다.
--------------- 원래의 질문 ----------------- 내가 포트란에서 동적 배열을 사용할 필요가
처음에는 어레이의 정확한 크기를 예측할 수없는 경우를 대비하여 90입니다. 내가 여기서 뭔가를 놓치고 경우
subroutine DArray()
double precision, dimension(:), allocatable :: list
allocate(list(1))
list(1) = 1.1
call AddToList(list, 2.2)
call AddToList(list, 3.2)
call AddToList(list, 4.2)
call AddToList(list, 5.2)
print *, list(1)
print *, list(2)
print *, list(3)
print *, list(4)
print *, list(5)
end
subroutine AddToList(list, element)
double precision :: element
double precision, dimension(:), allocatable :: list
double precision, dimension(:), allocatable :: clist
if(allocated(list)) then
isize = size(list)
allocate(clist(isize+1))
do i=1,isize
clist(i) = list(i)
end do
clist(i+1) = element
deallocate(list)
allocate(list(isize+1))
do i=1,isize+1
list(i) = clist(i)
end do
deallocate(clist)
end if
end
그래서 누구나 볼 않습니다 그래서 때마다 새로운 요소가 배열의 끝에 추가됩니다 할당 가능한 배열 확장해야 코드를 썼다? [해답 - 맨위의 답안과 전체 코드보기]
어떤 문제/오류가 있습니까? – agentp
프로그램이'deallocate (list)'에 멈추고 더 이상 아무것도 실행되지 않습니다. 인쇄 된 오류는 없으며 그냥 멈추고 아무것도하지 않습니다. – chanfort
아마도 Fortran 90을 의미하는 것이 아니라'DArray'에서'AddToList'를 위해 [explicit interface] (http://stackoverflow.com/q/13058743/3157076)를 사용할 수 있어야합니다 : 할당 가능한 더미 인수가 있습니다. – francescalus