Re: confusing segfault



C. G. Montgomery wrote:
It's confusing me, anyhow. I've pared down the program until it does
hardly anything -- except segfault:

/u01/home/cgm/QPO/wavea [582]$ cat stepcheck2.f
c stepcheck2 segfaults

subroutine step(x)
implicit none
integer x, delta
write(*,*) 'in step; x= ',x
delta = 0
write(*,*) 'delta set to ',delta
x=x+delta
write(*,*) x
RETURN
END
ccccccccccccccccccccccc
program stepcheck
implicit none
integer x, seed(3), j, count(3)
real rnd
c seed random number generator
call itime(seed)
rnd=rand(seed(1)*seed(2)*seed(3))

DO 5, j=1,3
5 count(j)=0
WRITE(*,*) (count(j),j=1,3)

DO 10, j=1,10
call step(0)

You probably want "x" to be the argument to step.

Dick Hendrickson

count(x+2)=count(x+2) + 1
10 CONTINUE
WRITE(*,*) (count(j),j=1,3)
STOP
END

/u01/home/cgm/QPO/wavea [583]$ g77 stepcheck2.f -ostepcheck2 -O3 -Wall
stepcheck2.f: In program `stepcheck':
In file included from stepcheck2.f:0:
stepcheck2.f:16: warning: 'x' might be used uninitialized in this function
/u01/home/cgm/QPO/wavea [584]$ ./stepcheck2
0 0 0
in step; x= 0
delta set to 0
Segmentation fault

/u01/home/cgm/QPO/wavea [585]$ g77 -v
Reading specs from /usr/lib/gcc/i486-linux-gnu/3.4.6/specs
Configured
with: ../src/configure -v --enable-languages=c,c++,f77,pascal --prefix=/usr --libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --program-suffix=-3.4 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --with-tune=i686
i486-linux-gnu
Thread model: posix
gcc version 3.4.6 (Debian 3.4.6-6)

/u01/home/cgm/QPO/wavea [586]$ gfortran stepcheck2.f -ostepcheck2 -Wall
/u01/home/cgm/QPO/wavea [587]$ ./stepcheck2
0 0 0
in step; x= 0
delta set to 0
Segmentation fault

/u01/home/cgm/QPO/wavea [588]$ gfortran -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian
4.3.3-3' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.3 (Debian 4.3.3-3)
/u01/home/cgm/QPO/wavea [589]$

(The same thing happens if I leave off the -O3 for g77.) What makes it more puzzling is that a bigger program, using the more
complete version of step(x), called the same way, compiles and runs
successfully and gives reasonable results. I got into this problem when
making a special test program to exercise the subroutine as a check.

I'd greatly appreciate any comments on what is wrong, or how I might try
to track down the problem.

TIA cgm
.