使用常量参数调用函数时出现Segfault错误 [英] Segfault when calling a function with a constant argument
问题描述
程式su
隐式无
真实ran3
write(*,*)ran3(0)
结束程序su
real * 8函数ran3(iseed)
隐式无
整数iseed
iseed = iseed * 153941 + 1
ran3 = float(iseed)* 2.328 + 0.5
end function ran3
我在编译时没有问题,但是当我执行代码时,我收到以下消息:
编程接收到的信号SIGSEGV:分段错误 - 无效的存储器引用。
Backtrace for this error:
#0 0xB76BAC8B
#1 0xB76BB2DC
#2 0xB77BA3FF
#3 0x8048653 in ran3_
#4 0x80486B3在MAIN__ der.f90:?
分段错误(核心转储)
请问为什么,以及如何解决它是什么?
我看到代码存在两个问题。第一个是我认为是错误原因的。函数 ran3
被常量 0
引用为实际参数,但相应的伪参数 iseed
用于函数中赋值语句的左侧。这是一个错误:你不能改变零的值。第二个错误是ran3返回一个 real * 8
(不管怎么说;这是一个非标准的声明),但在主程序 ran3
中声明为默认真实
。
使用gfortran 4.7.2编译以下程序和函数。
程序su
隐含无
real :: ran3
write(*,*)ran3(0)
结束程序su
函数ran3(iseed)
隐式无
integer :: iseed,temp
real :: ran3
temp = iseed * 153941 + 1
ran3 = temp * 2.328 + 0.5
结束函数ran3
I have written this very simple code in Fortran:
program su
implicit none
real ran3
write(*,*) ran3(0)
end program su
real*8 function ran3(iseed)
implicit none
integer iseed
iseed=iseed*153941+1
ran3=float(iseed)*2.328+0.5
end function ran3
I have no problem in compiling it but when I execute the code I get this message:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0xB76BAC8B
#1 0xB76BB2DC
#2 0xB77BA3FF
#3 0x8048653 in ran3_
#4 0x80486B3 in MAIN__ at der.f90:?
Segmentation fault (core dumped)
Could you please tell why, and how I can solve it?
I see two problems with the code. The first is the one which I think is the cause of the error. The function ran3
is referenced with the constant 0
as the actual argument, but the corresponding dummy argument iseed
is used on the left side of an assignment statement in the function. This is an error: you can't change the value of zero.
The second error is that ran3 returns a real*8
(whatever that may be; it's a non-standard declaration), but in the main program ran3
is declared as being a default real
.
The following program and function compile with gfortran 4.7.2.
program su implicit none real :: ran3 write(*, *) ran3(0) end program su function ran3(iseed) implicit none integer :: iseed, temp real :: ran3 temp = iseed * 153941 + 1 ran3 = temp * 2.328 + 0.5 end function ran3
这篇关于使用常量参数调用函数时出现Segfault错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!