發生
同樣的行爲因此,我可以建議是爲了處理數據使用重載的子程序 - 那麼你將有通用的行爲,而不需要將維度參數明確地傳遞給函數(從而擺脫警告)。然後,我建議您遵循Holmz關於在測試階段使用所有警告的建議,然後在生產構建期間將其完全關閉(-w)。現在我無法找到抑制此警告的有效方法(除了-w) - 似乎默認打開數組邊界檢查並且未覆蓋-fno-bounds-check或-Wno-array -bounds。但重載函數可以更好的解決您的問題,實施應該是這樣在這種情況下:
module functions
implicit none
interface test_dim
module procedure test_func1d, test_func2d, test_func3d
end interface ! test_dim
contains
subroutine test_func1d(input1d)
real, intent(in) :: input1d(:)
print*, "DOING 1 DIM"
print*, "SHAPE OF ARRAY:", shape(input1d)
end subroutine test_func1d
subroutine test_func2d(input2d)
real, intent(in) :: input2d(:,:)
print*, "DOING 2 DIM"
print*, "SHAPE OF ARRAY:", shape(input2d)
end subroutine test_func2d
subroutine test_func3d(input3d)
real, intent(in) :: input3d(:,:,:)
print*, "DOING 3 DIM"
print*, "SHAPE OF ARRAY:", shape(input3d)
end subroutine test_func3d
end module functions
program test_prog
use functions
implicit none
real :: case1(10), case2(20,10), case3(30, 40, 20)
call test_dim(case1)
call test_dim(case2)
call test_dim(case3)
end program test_prog
並通過此功能產生的輸出是這樣的:
DOING 1 DIM
SHAPE OF ARRAY: 10
DOING 2 DIM
SHAPE OF ARRAY: 20 10
DOING 3 DIM
SHAPE OF ARRAY: 30 40 20
將gfortran與-fbounds-check設置爲默認值並不常見。您的編譯時檢查是您應該在源代碼中修復的另一個問題。 – tim18
我沒有構建gfortran,我使用Ubuntu 16.04上的'sudo apt-get install gfortran'來安裝它。當然,這表明源代碼必須有一些東西。我會嘗試刪除gfortran並重新安裝它 - 這沒有什麼區別。 – 1QuickQuestion
它可能不會在編譯時壓制檢查。除了增加運行時間之外,邊界檢查總是很好用。編譯器和計算機現在如此之快,爲什麼會擔心增加編譯時間? –