2012-12-10 31 views
1

UPDATE:我修改後的代碼看起來是這樣的:Fortran子例程的輸入參數是否可以在子例程的主體中解除分配和分配?

program run_module_test 
    use module_test 
    implicit none 

    TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:) 

    call update(xyzArray) 
    write(6,*)'xyzArray',xyzArray 

end program run_module_test 

module module_test 
    implicit none 

    TYPE :: newXYZ 
     real(4) :: x, u 
     real(4) :: y, v 
     real(4) :: z, w 
     real(4),dimension(3) :: uvw  
    END TYPE 

    integer(4) :: shape = 3 

contains 

    subroutine update(xyzArray) 

    integer(4) :: i 
    TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:) 
    allocate(xyzArray(shape)) 

    do i = 1, shape 
     xyzArray(i)%x = 0 
     xyzArray(i)%y = 0 
     xyzArray(i)%z = 0 
     xyzArray(i)%u = 0 
     xyzArray(i)%v = 0 
     xyzArray(i)%w = 0 
     xyzArray(i)%uvw = (/0,0,0/) 
    end do 
    return 
    end subroutine update 

end module module_test 

當他們被編譯,它們產生類似的錯誤:

TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:) 
                1 
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1) 

當我消除更新()子程序的說法,我收到了矛盾的錯誤:

TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:) 
                 1 
Error: Symbol at (1) is not a DUMMY variable 

我是否已排除了有用建議中指出的錯誤來源?這可能是一個編譯器相關的錯誤(使用mpi90)?

~~~第一編輯~~~ 我有一個子程序,其輸入參數是用戶定義類型XYZ的數組。我希望取消分配xyzArray,並在子例程的主體中將其分配/修改爲不同的大小。我試圖通過changing array dimensions in fortran建議的方法,但是當我做到以下幾點:當我嘗試

Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1) 

subroutine update(xyzArray, ...) 
... 
deallocate(xyzArray(myshape)) 
allocate(xyzArray(newshape)) 

我收到錯誤消息

subroutine update(xyzArray, ...) 
... 
TYPE (XYZ), allocatable :: xyzArray(:) 

我收到一條錯誤消息:

Error: Expression in DEALLOCATE statement at (1) must be ALLOCATABLE or a POINTER 
Error: Expression in ALLOCATE statement at (1) must be ALLOCATABLE or a POINTER 

我需要做什麼來改變子程序中數組的大小?

回答

5

要做到這一點:

  • 僞參數必須是可分配的。可分配的僞參數需要一個實現Fortran 2003標準(或實現所謂的「可分配」TR的Fortran 95編譯器)相關部分的編譯器。

  • 需要一個過程的顯式接口(該過程必須是一個模塊過程,一個內部過程或在調用範圍內有一個接口塊)。

  • 僞參數不能是意圖(in)。如果在子例程中根本不使用僞參數值的分配狀態或其他方面,那麼intent(out)可能是合適的(如果預先分配,在調用該過程時將自動釋放僞參數),否則意圖(inout)或無意。

(你的代碼示例第二塊具有與DEALLOCATE語句語法錯誤 - 你應該簡單地指定xyzArray變量,請關閉(myshape)形狀規格))

例如,在一個模塊中:

subroutine update(xyzArray) 
    type(xyz), allocatable, intent(inout) :: xyzArray(:) 
    ... 
    if (allocated(xyzArray)) deallocate(xyzArray) 
    allocate(xyzArray(newshape)) 
    ... 
3

如果你確定,要解除您的子程序數組,可以聲明瞭僞參數,是意圖(出來),所以它是自動釋放進入子程序時:

module whatever 
    implicit none 

    type :: xyz 
    : 
    end type xyz 

    contains 

    subroutine update(xyzArray) 
     type(xyz), allocatable, intent(out) :: xyzArray(:) 
     : 
     allocate(xyzArray(someshape)) 
     : 
    end subroutine update 

    end module whatever 

如已經在IanH中指出的那樣,該過程必須具有明確的接口(例如,被包含在模塊中),並且在調用者程序中,您必須聲明實際參數可分配:

program test 
    use whatever 
    implicit none 

    type(xyz), allocatable :: array(:) 
    : 
    call update(array) 
    : 
end program test 
相關問題