### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: a beginner's segmentation fault  (Read 560 times)

#### veselic

• New Member
• Posts: 13
##### a beginner's segmentation fault
« on: March 07, 2024, 06:44:53 pm »
Dear pascallers,
I got
Segmentation fault (core dumped)

to the following simple program

CONST NMAX = 1000;
TYPE NRANGE=1..NMAX;
MATRIX=ARRAY[NRANGE,NRANGE] OF DOUBLE;
var N,M: NRANGE; A: MATRIX;

procedure transp(var n,m:nrange; a: matrix);
var i,j : nrange;
z : matrix;
begin
for i := 1 to n do
for j := 1 to n do
z[i,j] := a[j,i];
a := z;
end;
begin
a[1,1] := 1; a[1,2] := 2; a[1,3] := 3;
a[2,1] := 4; a[2,2] := 5; a[2,3] := 6;
transp(n,m,a)
end.

Thanks in advance for help, K. Veselic

#### Bart

• Hero Member
• Posts: 5296
##### Re: a beginner's segmentation fault
« Reply #1 on: March 07, 2024, 07:15:00 pm »
You do not initialize n and m, and you don't use m in transp.
(Well, in this example, since the are global variables to the main program, they will be zero. This wil reduce the transp procedure to a := z, since none of the loops will be executed.)

Also, the z := a assigment does not what you seem to expect, since a is passed as a value parameter instead of a var parameter. Therefore a is treated as a copy of the original a, and all changes are made to that copy.
So, after the transp() procedure a will remain unchanged.

Bart
« Last Edit: March 07, 2024, 07:25:33 pm by Bart »

#### Bart

• Hero Member
• Posts: 5296
##### Re: a beginner's segmentation fault
« Reply #2 on: March 07, 2024, 07:29:17 pm »
Notice that the NumLib package has code to transpose a matrix of floats.
Code: [Select]
`uses  typ, omv;var  A: array[1..m, 1..n] of ArbFloat;  //type ArbFloats is defined in unit typ and it defaults to be double IIRC.  B: array[1..n, 1..m] of ArbFloat;begin  ... some code to fill A  omvtrm(    A[1,1], m, n, n,    B[1,1], m  );end.`
Bart

#### alpine

• Hero Member
• Posts: 1110
##### Re: a beginner's segmentation fault
« Reply #3 on: March 07, 2024, 07:33:55 pm »
IMHO the MATRIX is quite big (1M*SizeOf(Double)) to be placed into the stack twice.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000