Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Request Help with Segmentation Fault

5 views
Skip to first unread message

Michael C.

unread,
Feb 22, 2019, 5:48:46 AM2/22/19
to

I got a little bored and decided to play around with fortran. I only
used it for about 3-4 months back in 1987, and remember very little.

I put together a short guess the number game. The game worked fine
before I modified the source to allow the game to be repeated.

Compiled with gfortran 6.1 on OS X 10.11.6:
$ gfortran hilo.f90 -o hilo
$

$ ./hilo
02/22/2019 04:55:40

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0 0x102fcc729
#1 0x102fcbaf5
#2 0x7fff87ca5529
Segmentation fault: 11

$

If I uncomment the line:
! write (*,*) 'random number set : ', num

the program works as expected. Obviously it isn't useful to be told
num, since computing the target is trivial given the random number
provided.

Can anyone explain what I'm doing wrong that causes the program to
segfault when not writing num to the display?

$ cat hilo.f90
program hilo
implicit none
integer, parameter :: limit = 100
integer :: count, guess, target
real :: num
integer, dimension (16) :: seed=0
integer, dimension (3) :: now, date
character :: again

call idate(date)
call itime(now)
write (*,'(XI2.2AI2.2AI4XI2.2AI2.2AI2.2)') date(2), '/', date(1), '/', &
date(3), now(1), ':', now(2), ':', now(3)

! random_number needs a random seed or it will always use the same sequence
! for lack of a better way, the current second will do
! Should probably be an implied do loop but I forget the syntax
do count=0, 15
seed(count)=now(3)
end do

call random_seed(put=seed)

do
call random_number(num)
! write (*,*) 'random number set : ', num
target = int(limit * num + 1)
count = 0
do
write (*,*) "Enter your guess between 1 and ", limit
read (*,*) guess
count = count+1

if (guess.eq.target) then
write (*,*) "You guessed it in ", count, " tries."
exit
else if (guess.gt.target) then
write (*,*) "Your guess is too high."
else
write (*,*) "Your guess is too low."
end if
end do

write (*,*)
write (*,*) "Would you like to play again?"
read (*,*) again
if ((again.eq.'n').or.(again.eq.'N')) exit
end do
end program hilo

$

Any help appreciated.

Thanks,

Michael J. Chappell
--
mjcha...@verizon.net http://mcsuper5.freeshell.org/

The two most abundant things in the universe are hydrogen and
stupidity. -- Harlan Ellison

Michael C.

unread,
Feb 24, 2019, 2:02:50 PM2/24/19
to
0 new messages