SRL Archive - OSR Help

Access violation :(


Foundry

2013-11-20 22:06:31

program Grapher;
{$i srl-6/srl.simba}

type YEquals = record
TextForm: String;
XModifier: Double;
YModifier: Double;
end;

function solveForX(x: Integer; Equation: YEquals): Double;
begin
result := (x * Equation.XModifier) + Equation.YModifier;
end;

function makeTPA(SolveFor: YEquals; XRange, YRange: TIntegerArray): TPointArray;
var
x, y, i: Integer;
tpa: TPointArray;
begin
i := 0;
x := XRange[0];
for x to XRange[1] do
begin
tpa[i] := point(x, round(solveForX(x, SolveFor)));
i := i + 1;
end;
end;

procedure graph(Points: TPointArray);
var
i = 0;
begin
WriteLn('Graphing.');
for i to high(Points) do
mouse(Points[i], MOUSE_LEFT);
end;

begin
makeTPA(['3x-2', 3, -2], [0, 10], [0, 10]);
end.

I just included the latter part because I was in a hurry. I keep getting access violation at line 24 column 22. I have tried almost everything but haven't got it to work. I am trying to create something that draws the equation of a line in paint.

Compiled successfully in 858 ms.
Exception in Script: Runtime error: "Access violation" at line 24, column 22

Flight

2013-11-20 23:31:32

I might be wrong but I made a couple mods to your "makeTPA()" function, try this out:

function makeTPA(SolveFor: YEquals; XRange, YRange: TIntegerArray): TPointArray;
var
ex : extended;
x, y, i: Integer;
tpa: TPointArray;
begin
i := 0;
x := XRange[0];
for x to XRange[1] do
begin
ex := solveForX(x, SolveFor);
setArrayLength(result, Length(result)+1);
result[High(result)] := point(x, round(ex));;
i := i + 1;
end;
end;

Foundry

2013-11-21 08:30:10

From: Flight
I might be wrong but I made a couple mods to your "makeTPA()" function, try this out:

function makeTPA(SolveFor: YEquals; XRange, YRange: TIntegerArray): TPointArray;
var
ex : extended;
x, y, i: Integer;
tpa: TPointArray;
begin
i := 0;
x := XRange[0];
for x to XRange[1] do
begin
ex := solveForX(x, SolveFor);
setArrayLength(result, Length(result)+1);
result[High(result)] := point(x, round(ex));;
i := i + 1;
end;
end;


Thanks, but why exactly was I getting an access violation on that line?

riwu

2013-11-21 08:46:03

From: HardcorBoter
Thanks, but why exactly was I getting an access violation on that line?

You were accessing tpa[0] (which is only possible if the length is at least one) but the length of tpa is 0.
Remember, for general arrays the index starts with 0 ;)

bg5

2013-11-21 08:48:41

From: HardcorBoter
Thanks, but why exactly was I getting an access violation on that line?



you didn't set length of your tpas.

Foundry

2013-11-21 09:35:49

From: bg5
you didn't set length of your tpas.



Oh, didn't know I had to do that, thanks.

Flight

2013-11-21 20:58:27

From: bg5
you didn't set length of your tpas.


I didn't know for sure this would be the cause of that particular error. I found Access Violation errors usually result from grouping too many functions into a single line, for example:

sortTPAFrom(Point(Box.X1-(Player(getMyPlayer).getBasePnt), y)));
Where as you might need to store the results from these functions in variables first, then plug the variables into the procedure;

x := Box.X1-(Player(getMyPlayer).getBasePnt);
sortTPAFrom(Point(x, y));
I've had trouble with that in the past. Usually not setting the length of an array will give you an Out of Range error (when your loop hits a non-existent index in that array), rather than Access Violation.

Olly

2013-11-21 22:20:44

From: Flight
I didn't know for sure this would be the cause of that particular error. I found Access Violation errors usually result from grouping too many functions into a single line, for example:

sortTPAFrom(Point(Box.X1-(Player(getMyPlayer).getBasePnt), y)));
Where as you might need to store the results from these functions in variables first, then plug the variables into the procedure;

x := Box.X1-(Player(getMyPlayer).getBasePnt);
sortTPAFrom(Point(x, y));
I've had trouble with that in the past. Usually not setting the length of an array will give you an Out of Range error (when your loop hits a non-existent index in that array), rather than Access Violation.


Lape will access violate, pascalscript will out of range. I think this is because pascalscript checks the length, and lape doesn't.

bonsai

2013-11-21 22:22:42

From: Flight
Usually not setting the length of an array will give you an Out of Range error (when your loop hits a non-existent index in that array), rather than Access Violation.


I get mostly access errors when borking array lengths.

program new;
var ia: array of integer;

begin
ia[1] := 20;
end.


Compiled successfully in 296 ms.
Exception in Script: Runtime error: "Access violation" at line 5, column 9

Flight

2013-11-21 23:21:14

From: Olly
Lape will access violate, pascalscript will out of range. I think this is because pascalscript checks the length, and lape doesn't.


That makes sense on past errors. Thanks for the info.