Recent

Author Topic: External: SIGSEGV  (Read 1820 times)

tgd

  • Newbie
  • Posts: 2
External: SIGSEGV
« on: September 22, 2015, 12:34:19 pm »
Hi, I'm not sure if this is the right section for this, but here it is:
Code: [Select]
program project1;
var
a,b,n:integer;
mat:array of array of real;
F:textfile;
begin
  write('n=');
  readln(n);
setlength(mat,n,n);
assign(F,'C:\Users\Julius\Desktop\fps\aaa.txt');
reset(F);
for a:=1 to n do
    for b:=1 to n do
        read(F,mat[a,b]);
close(F);
end.

The program is supposed to compute the inverse matrix to the matrix from the text file. I actually wrote the whole thing and it works, but now I'm trying to use dynamic arrays. When I run the program, I choose the n value, press enter and get an error message saying that project1 raised exception class External:SIGSEGV at the line with 'read(F,mat[a,b]);'.
Any help would be appreciated. Thanks.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: External: SIGSEGV
« Reply #1 on: September 22, 2015, 01:14:12 pm »
The indices in a dynamic array run from 0 to length-1, not from 1 to length. The safest way to iterate over all array positions, which works with any kind of array, is to always use something like

Code: [Select]
for a:=low(mat) to high(mat) do
  for b:=low(mat[a]) to high(mat[a]) do
     ...

tgd

  • Newbie
  • Posts: 2
Re: External: SIGSEGV
« Reply #2 on: September 22, 2015, 01:27:09 pm »
It works now, thanks a lot!

 

TinyPortal © 2005-2018