[fpc-pascal] Re: Problems with assigning pointers
Darius Blaszyk
dhkblaszyk at zeelandnet.nl
Tue Apr 10 22:06:35 CEST 2012
Here's a minimal example that has the bug. First of all the first output is wrong as it says 0,50 instead of 20,50 (so the x item is overwritten). The second printed output differs from the first, so again the x item is overwritten. I'm clueless.
program test;
{$mode objfpc}{$H+}
type
TVertex = record
x: double;
y: double;
end;
PVertex = ^TVertex;
TEdge = record
v1: PVertex;
v2: PVertex;
end;
var
vert_count: integer = 0;
vert_list: array of TVertex;
edge_count: integer = 0;
edge_list: array of TEdge;
function add_vert(x, y: double): PVertex;
begin
Inc(vert_count);
SetLength(vert_list, vert_count);
vert_list[vert_count - 1].x := x;
vert_list[vert_count - 1].y := y;
Result := @vert_list[vert_count - 1];
end;
procedure add_edge(v1, v2: PVertex);
begin
Inc(edge_count);
SetLength(edge_list, edge_count);
WriteLn(v1^.x,' ',v1^.y); // this
edge_list[edge_count - 1].v1 := v1;
WriteLn(v1^.x,' ',v1^.y); // outputs the same thing as this
edge_list[edge_count - 1].v2 := v2;
end;
var
v1: PVertex;
v2: PVertex;
begin
v1 := add_vert(20, 50);
v2 := add_vert(220, 50);
add_edge(v1, v2);
readln;
end.
Regards, Darius
On Apr 10, 2012, at 7:04 PM, leledumbo wrote:
> Can't reproduce with the following program:
>
> type
> TVertex = record
> x: double;
> y: double;
> end;
> PVertex = ^TVertex;
>
> TEdge = record
> v1: PVertex;
> v2: PVertex;
> end;
>
> var
> edge_count: LongWord = 0;
> edge_list: array of TEdge;
>
> procedure add_edge(v1, v2: PVertex);
> begin
> inc(edge_count);
> SetLength(edge_list, edge_count);
> WriteLn(v1^.x:1:4,' ',v1^.y:1:4); // this
> edge_list[edge_count - 1].v1 := v1;
> WriteLn(v1^.x:1:4,' ',v1^.y:1:4); // outputs the same thing as this
> edge_list[edge_count - 1].v2 := v2;
> end;
>
> var
> a: PVertex;
> begin
> New(a);
> a^.x := 2.56;
> a^.y := 3.14;
> add_edge(a,nil);
> Dispose(a);
> end.
>
>
> --
> View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Problems-with-assigning-pointers-tp5630400p5630539.html
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
More information about the fpc-pascal
mailing list