[fpc-pascal] Re: Problems with assigning pointers

J├╝rgen Hestermann juergen.hestermann at gmx.de
Wed Apr 11 11:43:46 CEST 2012


Vincent Snijders schrieb:
 >> type
 >>  TVertex = record
 >>    x: double;
 >>    y: double;
 >>  end;
 >>  PVertex = ^TVertex;
 >>
 >>  TEdge = record
 >>    v1: PVertex;
 >>    v2: PVertex;
 >>  end;
.
.
 >>  Result := @vert_list[vert_count - 1];
 >
 > I think this is not correct. If you increase the size of vert_list,
 > then the array may be relocated and Result isn't a valid pointer
 > anymore.

True.

I think it would be better to change the type of "vert_list" to

type vert_list : array of PVertex;

This would have two advantages:

1.) Array sorting and reallocation would be faster because only a 
pointer to each element is stored in the array and if the element type 
is changed (increases in size) it would not affect performance.

2.) Assignments as "Result := @vert_list[vert_count - 1];" would convert 
to "Result := vert_list[vert_count - 1];" and therefore references to 
the element itself would be unaffected by array resorting.




More information about the fpc-pascal mailing list