2013-10-28 48 views
2

我非常驚訝地發現,由於數組大小不匹配,使用gcc v。4.4.6構建的代碼無法運行。當使用gcc v。4.7.3構建時,它工作得很好。我創建了一個最小的工作爲例,說明問題的根源:此功能是否在最近的Fortran標準中提供?

program main 

    implicit none 

    integer, allocatable, dimension(:,:) :: array_a 
    integer, allocatable, dimension(:,:) :: array_b 

    allocate(array_a(5,2)) 
    allocate(array_b(2,1)) 

    array_a = 1 

    array_b = array_a 

    print *, array_a 
    print *, array_b 

end program main 

當用gcc 4.4.6 v建成,它在運行時錯誤崩潰:

At line 13 of file main.f90 Fortran runtime error: Array bound mismatch, size mismatch for dimension 1 of array 'array_b' (1/4)

當編譯用gcc v 4.7.3,它產生如下輸出:

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

注意,它會自動調整'array_b'的大小以匹配'array_a'的大小。這是一個由我看到的新的Fortran標準提供的'特性'嗎?

回答

6

您正在使用Fortran 2003功能 - 在分配時自動重新分配陣列。它還沒有在gcc-4.4中實現。

此功能意味着分配給不合格形狀的數組b被自動重新分配到賦值右側的形狀。對於Fortran 2003功能,您必須使用最近的編譯器版本(不僅是GCC)。

0

正如弗拉基米爾說,這是2003年的Fortran的功能。如果你看看2003 working document你會看到第7.4.1.3,

If variable is an allocated allocatable variable, it is deallocated if expr is an array of different shape or any of the corresponding length type parameter values of variable and expr differ. If variable is or becomes an unallocated allocatable variable, then it is allocated with each deferred type parameter equal to the corresponding type parameters of expr, with the shape of expr, and with each lower bound equal to the corresponding element of LBOUND(expr) .

注7.36顯示,如果你想array_b以保持其形狀,你需要聲明的行

array_b(1:2,1) = array_a(3:4,2) 
你想要的任何 array_a元素的

相關問題