Board index » delphi » dynamic allocate the size of an array ?

dynamic allocate the size of an array ?

Hi there
Try something like:

uses WinTypes, WinProcs, WinCrt;
type
TPtArray = array[0..0] of TPoint;
PPtArray = ^TPtArray;

function AskPointsCount: word;
var
  NumPts: word;
begin
  {A function you create to get}
  {an idea of how many points the}
  {user wants}
  write('How many points? :');
  readln(NumPts);
  AskPointsCount := NumPts;
end;

procedure GetSinglePoint(var APoint: TPoint);
begin
  {A function you create to get}
  {all points user wants, one at a time}
  write('Pt.X: ');
  readln(APoint.X);
  write('Pt.Y: ');
  readln(APoint.Y);
end;

function GetPoints(var PtArray: PPtArray): word;
var
  NumItems: word;
  i: word;
begin
  NumItems := AskPointsCount;
  GetPoints := 0;
  GetMem(PtArray, SizeOf(TPoint) * NumItems);
  if PtArray = nil then exit;
  for i := 0 to NumItems -1 do
    GetSinglePoint(PtArray^[i]);
  GetPoints := NumItems;
end;

procedure DisposePoints(var PtArray: PPtArray; NumPts: word);
begin
  FreeMem(PtArray, (NumPts * SizeOf(TPoint)));
  PtArray := nil;
end;

var
  PtArray: PPtArray;
  NumPts: word;
  SomeDC: hDC;
  i: word;
begin
  NumPts := GetPoints(PtArray);
  clrscr;
  for i := 0 to numpts -1 do
  begin
    writeln('Your selections are: ');
    writeln('Point: ', i, ' ', PtArray^[i].X, ':',
PtArray^[i].Y);
  end;

  if PtArray <> nil then
  begin
    SomeDC := GetDC(SomeWindow);
    {Where somewindow is a valid window handle}
    Canvas.Polygon(SomeDC, PtArray^, NumPts);
    {Not being a Delphi user, I assume this is what your}
    {call looks like. If an array is required instead of a}
    {pointer to an array, then use ...Polygon(PtArray^)}
    {This is the Polygon method used in TPW/BP}
    DisposePoints(PtArray, NumPts);
    ReleaseDC(SomeWindow, SomeDC);
  end
  else
  begin
    writeln('Not able to allocate memory');
    {Unable to get pointer to array}
  end;
end.

Hop this helps.
--

Regards
James Burroughs
Internet: Burro...@Telkom09.Telkom.Co.Za

 

Re:dynamic allocate the size of an array ?


In article <45vlgg$...@grovel.iafrica.com>,
James Burroughs  <burro...@telkom09.telkom.co.za> wrote:
Quote
>Hi there
>Try something like:

[ lots of good code deleted to save bandwidth ]
Quote

>Hop[e] this helps.
>--

>Regards
>James Burroughs
>Internet: Burro...@Telkom09.Telkom.Co.Za

Good post! Thanks, James!

Unfortunately, James' method shares the limitation common to all
schemes which model the array as a big block of memory: the total
_size_ of all elements must be less than 65520 bytes because of
the 64K limit.

If you model the array as a block of pointers, you can call getmem
once for each pointer, so the total size of the array elements
can exceed 64K. In this case the _number_ of elements is limited
to 16K, because all the pointers must fit in a 64K block.

If you need more than 16K elements, then it gets a bit trickier.
I have some code which will do this -- anyone needing it should
e-mail me with a request for same.

--
/---------------------------------------------------------------------\
| Perry Domzella                                    ( pe...@mks.com ) |
|---------------------------------------------------------------------|
|  ( 2 * wrong <> 1 * right ) { but } ( 3 * right = 1 * left ) {;-)}  |

Other Threads