How to use dynamic array in free pascal

Dynamic arrays can be allocated, expanded, shrinked or deallocated at run time using SetLength procedure.

Example:


var
  MyArray: array of Integer;
  i: Integer;
begin
  SetLength(MyArray, 20);
  for i:= 0 to High(MyArray) do
  MyArray[i]:= Random(20);

  for i:= 0 to High(MyArray) do
  Writeln(MyArray[i]);

  SetLength(MyArray, 0); // Deallocate
end.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s