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

signal SIGSEGV: Segmentation fault - invalid memory reference

5,942 views
Skip to first unread message

Nat

unread,
Jan 22, 2016, 12:03:42 PM1/22/16
to
Hi,

I execute a program with gfortran in a MAC OS.

The program opens a file which contains 24 columns and 40000 lines, reads the file and writes in another file the sum of the 24 columns for each line.

For the first five lines the program works properly and then stops returning the error "Program received signal SIGSEGV: Segmentation fault - invalid memory reference"

Is there any way to solve this?

Nat

mecej4

unread,
Jan 22, 2016, 12:08:42 PM1/22/16
to
To answer, we need to see at least the READ and Format statements, if
used, and the declarations of the variables that are read into. Have you
noted that for doing what you described the memory need only have space
for 25 REAL or INTEGER variables?

--
mecej...@nospam.invalid
(Replace four by 4, nospam by operamail, invalid by com,
and remove all underscores)

Richard Maine

unread,
Jan 22, 2016, 12:11:12 PM1/22/16
to
Yes. I'm sure there is.

Oh. If you perhaps wanted more details than that for an answer, then
you'll have to give more pertinent details of the problem. Like actually
showing the code in question. Knowing the number of lines and columns in
the file seems about as useful as knowing what day of the week you ran
the program on.

--
Richard Maine
email: last name at domain . net
domain: summer-triangle

Nat

unread,
Jan 22, 2016, 12:25:21 PM1/22/16
to
OK. Well, at fist I thought if I had to include any flag in the makefile. You can find below the code.Note that neither the output file is written properly. Thank you!!

integer :: ncellmax,ncell,write_ncell
real :: h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23,h24
real, allocatable, dimension(:) :: sum_hours, vct_h1,vct_h2,vct_h3,vct_h4 ......etc

allocate (sum_hours(ncellmax))
allocate (vct_h1(ncellmax))......

open(100,file=fname)
do ncell=1, ncellmax
read(100,'(24e10.2)') h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18, &
h19,h20,h21,h22,h23,h24
vct_h1(ncell)=h1
vct_h2(ncell)=h2
.....etc to vct_h24

sum_hours(ncell)=vct_h1(ncell)+vct_h2(cell)+....

write(50,'(1I1,A,e10.2)') (write_ncell,tab,sum_hours(write_ncell),write_ncell=1,ncellmax)

mecej4

unread,
Jan 22, 2016, 12:35:21 PM1/22/16
to
Your code snippets do not show any assignment of a value to 'ncellmax'.

Nat

unread,
Jan 22, 2016, 12:39:46 PM1/22/16
to
sorry I forgot to include this
ncellmax=41400

Gordon Sande

unread,
Jan 22, 2016, 1:01:07 PM1/22/16
to
The trivial solution is to only write correct programs. But I expect
your question
is more on how to find errors in existing programs.

The usual step one is to turn on all the debugging aids your compiler offers.
Segmentation errors suggest bad subscripts so the subscript checking aids
are certainly suggested.

To get help you need to show the code. Otherwise you will get whimsical
answers that are about as useless as your question.



Nat

unread,
Jan 22, 2016, 1:13:45 PM1/22/16
to
I have also included the code. Please find below
integer :: ncellmax,ncell,write_ncell
real :: h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23,h24
real, allocatable, dimension(:) :: sum_hours, vct_h1,vct_h2,vct_h3,vct_h4 ......etc

allocate (sum_hours(ncellmax))
allocate (vct_h1(ncellmax))......

open(100,file=fname)
do ncell=1, ncellmax
read(100,'(24e10.2)') h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18, &
h19,h20,h21,h22,h23,h24
vct_h1(ncell)=h1
vct_h2(ncell)=h2
.....etc to vct_h24

sum_hours(ncell)=vct_h1(ncell)+vct_h2(cell)+....

write(50,'(1I6,A,e10.2)') (write_ncell,tab,sum_hours(write_ncell),write_ncell=1,ncellmax)

Nat

unread,
Jan 22, 2016, 1:44:35 PM1/22/16
to
On Friday, January 22, 2016 at 7:35:21 PM UTC+2, mecej4 wrote:
Aslo, I tried to include the flags -fbacktrace -fcheck=all and returned the error

Fortran runtime error: Index '1' of dimension 1 of array 'vct_h1' above upper bound of 0
This is referred to the line : vct_h1(ncell)=h1

I cannot understand the error. Any help with this?

steve kargl

unread,
Jan 22, 2016, 1:50:27 PM1/22/16
to
The above is only *a part* of the code. It can not be compiled by any Fortran compiler.
Expecting others to fill in the missing code seems to be rather odd. As pointed out
by others, ncellmax has no value.

--
steve








James Van Buskirk

unread,
Jan 22, 2016, 1:50:38 PM1/22/16
to
"Nat" wrote in message
news:387860c6-6941-4fd1...@googlegroups.com...

> Aslo, I tried to include the flags -fbacktrace -fcheck=all and returned
> the error

> Fortran runtime error: Index '1' of dimension 1 of array 'vct_h1' above
> upper bound of 0
> This is referred to the line : vct_h1(ncell)=h1

> I cannot understand the error. Any help with this?

Probably means you allocated vct_h1 before you set the value of
ncellmax.

Nat

unread,
Jan 22, 2016, 2:19:36 PM1/22/16
to
Yes you are right, I had set the value ncellmax after the allocation statement.Thank you very much!

baf

unread,
Jan 22, 2016, 2:33:03 PM1/22/16
to
Also, typo in the following assignment statement

sum_hours(ncell)=vct_h1(ncell)+vct_h2(cell)+....

where you reference "cell" rather than ncell.

It is hopeless for us to try and help you without the actual code being
given.

Walt Brainerd

unread,
Jan 22, 2016, 2:43:37 PM1/22/16
to
Without really looking at the code very carefully, I can suggest
that if you need variables h1, h2, ..., h24, you probably
should be thinking of an array. Same with vct_h1, vect_h2, etc.

And look up the SUM intrinsic function. It should help you.

robin....@gmail.com

unread,
Jan 23, 2016, 8:02:19 AM1/23/16
to
Yes, by showing the code.

robin....@gmail.com

unread,
Jan 23, 2016, 8:06:27 AM1/23/16
to
cell? should be ncell.

But this is still not the code.
It doesn't even show the entire loop.

robin....@gmail.com

unread,
Jan 23, 2016, 8:08:19 AM1/23/16
to
You need to show the code.

campbel...@gmail.com

unread,
Jan 23, 2016, 10:32:33 PM1/23/16
to
This is a possible code that would improve things a bit.
Also what version of gFortran, as line 6 is OPEN which fails with some versions of gFortran 5.1.0


character :: fname*128 = 'name_of_data_file.txt'
character :: tab = char(9)
integer :: ncellmax, ncell, write_ncell, iostat
real :: h(24)
real, allocatable :: sum_hours(:), vct_h(:,:)
!
ncellmax = 41400
allocate (sum_hours(ncellmax))
allocate (vct_h(ncellmax,24))
!
open (100,file=fname)
do ncell = 1,ncellmax
read (100,'(24e10.2)',iostat=iostat) h
if ( iostat/=0 ) exit
vct_h(ncell,:) = h
sum_hours(ncell)= sum (h)
end do
ncellmax = ncell-1
!
! open (50 ??
write (50,'(I0,A,es10.2)') (write_ncell,tab,sum_hours(write_ncell),write_ncell=1,ncellmax)
!
end
0 new messages