2011-12-07 32 views
0

在Linux環境下,我無法兩次使用pgf90 fortran編譯器調用相同的子例程。第一次調用該子程序是可以的,但第二次調用該子程序會導致分段錯誤。有人可以提出一些建議,我的代碼有什麼問題,舉例如下:Linux上pgf90編譯器第二次調用子例程的分段錯誤

P.S.與gfortran它是確定的,即使我嘗試了英特爾視覺FORTRAN,這是OK

program main 

use module_Append_1DI 

implicit none 

integer, allocatable:: Arr(:) 

integer::Brr(2) 

Brr=[3, 4] 

call Append_1DI(Arr,Brr) 

write(*,*)Arr 

call Append_1DI(Arr,Brr) 

write(*,*)Arr 

end program main 

module module_Append_1DI 

contains 

subroutine Append_1DI(A,B) 

implicit none 

!================================================ 

integer, allocatable, intent(inout)::A(:) 

integer, intent(in)::B(:) 

integer, allocatable::temp(:) 

integer::sizeA,sizeB,sizeN 

!================================================ 

sizeA=size(A); sizeB=size(B); sizeN=sizeA+sizeB 

allocate(temp(sizeN)); temp(1:sizeA)=A 

call move_alloc(from=temp,to=A) 

A(sizeA+1:sizeN)=B 

end subroutine Append_1DI 

end module module_Append_1DI 

回答

4

說實話,我很驚訝,它的工作原理你第一次調用它。這是因爲A未被分配,並且您不允許在未分配的可分配數組上使用內在大小。如果您打開所有的檢查標誌ifort告訴你這

Wot now? ifort --version 
ifort (IFORT) 12.0.4 20110427 
Copyright (C) 1985-2011 Intel Corporation. All rights reserved. 

Wot now? ifort -check all -g -traceback s.f90 
Wot now? ./a.out 
forrtl: severe (408): fort: (8): Attempt to fetch from allocatable variable A when it is not allocated 

Image    PC    Routine   Line  Source    
a.out    000000000046A3FA Unknown    Unknown Unknown 
a.out    0000000000468F75 Unknown    Unknown Unknown 
a.out    0000000000420B56 Unknown    Unknown Unknown 
a.out    0000000000404C95 Unknown    Unknown Unknown 
a.out    00000000004050E9 Unknown    Unknown Unknown 
a.out    0000000000402ED5 module_append_1di   24 s.f90 
a.out    000000000040385F MAIN__      46 s.f90 
a.out    0000000000402B2C Unknown    Unknown Unknown 
libc.so.6   00007FB5F826DEFF Unknown    Unknown Unknown 
a.out    0000000000402A29 Unknown    Unknown Unknown 

gfortran目前還不太清楚,但還是會告訴你的東西其實是錯誤的

Wot now? gfortran --version 
GNU Fortran (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 
Copyright (C) 2010 Free Software Foundation, Inc. 

GNU Fortran comes with NO WARRANTY, to the extent permitted by law. 
You may redistribute copies of GNU Fortran 
under the terms of the GNU General Public License. 
For more information about these matters, see the file named COPYING 

Wot now? gfortran -Wall -Wextra -pedantic -fbounds-check -std=f2003 -g -fbacktrace s.f90 
Wot now? ./a.out 
At line 24 of file s.f90 
Fortran runtime error: Array bound mismatch for dimension 1 of array 'temp' (1252015568/139957056323024) 

Backtrace for this error: 
    + function append_1di (0x400EC7) 
    at line 24 of file s.f90 
    + in the main program 
    at line 48 of file s.f90 
    + /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff) [0x7f4a4a1deeff] 

而且挑另一個隨機編譯器,從太陽/甲骨文,你再次得到相同的消息

Wot now? f90 -V 
f90: Sun Fortran 95 8.5 Linux_i386 2010/08/13 
usage: f90 [ options ] files. Use 'f90 -flags' for details 
Wot now? f90 -C s.f90 
Wot now? ./a.out 

****** FORTRAN RUN-TIME SYSTEM ****** 
Attempting to use an unallocated ALLOCATABLE 'A' 
Location: line 22 column 16 of 's.f90' 
Aborted 

所以問題是使用甲分配之前。

難道你認爲這是一個零大小的數組?那麼你需要從頭開始 - 一個未分配的alloctable數組根本沒有定義的大小,這與已分配的bu大小數組非常不同。

伊恩

+0

非常感謝伊恩,對於這樣的詳細回覆。我現在可以理解我的錯誤是什麼。 –