[fpc-pascal] Why use pointers to arrays?

Andrew Brunner andrew.t.brunner at gmail.com
Sun Oct 11 15:19:33 CEST 2009


In general I use pointers to data structures and especially when they
are arrays.

Take the following

PIntArray: ^TIntArray;

TIntArray:Array of Integer;


1st benefit:

declaring methods associated with classes before TIntArray needs to be
defined or declared.
  eg. procedure DoSomething(Var Data:TIntArray); vs (DataP:PIntArray);

Non-obvious benefit to all FPC users

FPC forces the ^ operator while accessing structures as pointers.
Delphi didn't force it and I even suspect that memory leaks can result
in NOT using the ^ to denote the reference to the pointer rather than
the pointer itself.

Lastly, passing by reference rather than by value is much faster.
Since copies need not be made when entering a method when entered
using a Pointer declaration vs DoSomething(Data:TIntArray).  In the
latter a copy of the calling Data array would be made, slowing the
application and using more memory - and for recursive methods lead to
the Stack running out of memory.



More information about the fpc-pascal mailing list