[fpc-pascal] Why use pointers to arrays?

Graeme Geldenhuys graemeg.lists at gmail.com
Sun Oct 11 10:38:59 CEST 2009


Hi,

The other "arrays" discussion got me thinking, and after working so
long with only desktop type applications that talk to database API's,
I long some knowledge of the lower lever Pascal features that was so
often used in Turbo Pascal days.

Anyway, I'm port an application that was written for OS/2 years ago
using SpeedPascal. I have already found quite a differences between
Free Pascal and SpeedPascal implementations of the "object pascal"
language.

Anyway to get to my questions, and using the code snippets shown
below. This code is all based on the code I am porting from
SpeedPascal. So maybe some strange usage is simply due to the age of
SpeedPascal etc..


----------------------
Type
  TLayoutLine = record
    Text: PChar;
    Length: longint;
    Height: longint;
    Width: longint;
    MaxDescender: longint;
    MaxTextHeight: longint;
    LinkIndex: longint;
    Style: TTextDrawStyle;
    Wrapped: boolean;
  end;


  TLinesArray = array[ 0..0 ] of TLayoutLine;
--------------------------



1)  Why would you use a pointer to an array and not simply a variable
what IS the array? In the program I am porting the following field
variable is defined.

  FLines: ^TLinesArray;

Why couldn't I simply say:

  FLines: TLinesArray;

Is there some advantage for using a pointer to the array, when passing
it around between procedures and functions?


2) The TLinesArray is a dynamic array, so shouldn't I maybe change the
ported code to define the array as follows, instead of the [0..0]
idea?

  TLinesArray = array of TLayoutLine;



3) Maybe the strange usage (to me at least) of dynamic arrays could be
explain in how the original program allocates memory for the array. In
the class where FLines is defined, it initially reserves ten empty
elements for the array in the class constructor.

  FAllocatedNumLines := 10;
  GetMem( FLines, FAllocatedNumLines * sizeof( TLayoutLine ) );

And then later if it needs more space for elements, it redefines the
array elements as follows:

Procedure TRichTextLayout.AddLineStart( Const Line: TLayoutLine );
var
  NewAllocation: longint;
begin
  if FNumLines >= FAllocatedNumLines then
  begin
    // reallocate the array twice the size
    NewAllocation := FAllocatedNumLines * 2;
    FLines := ReAllocMem(FLines, NewAllocation * sizeof(TLayoutLine));
    FAllocatedNumLines := NewAllocation;
  end;
  FLines^[ FNumLines ] := Line;    //  ***** (1)
  inc( FNumLines );
end;


Like I said, maybe this is related to different implementations of
object pascal. By doesn't one allocate memory (array elements) via the
SetLength() procedure?
eg:

  FAllocatedNumLines := 10;
  SetLength( FLines^, FAllocatedNumLines);


I marked on of the lines above with (1). I sometimes get "out of
range" errors there, so clearly somewhere in all this dereferencing of
arrays etc, I probably made a mistake somewhere in porting/translating
the SpeedPascal code to Free Pascal.

-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-pascal mailing list