Tuesday, April 17, 2012

Freeing Delphi Dynamic Arrays

This post is about freeing dynamic arrays in, Delphi.

In, Delphi, as in others, dynamic arrays useful in many instances. Here is a good link on a basic understanding
http://delphi.about.com/od/beginners/a/arrays.htm This is not what this post is about.

Let's declare a dynamic array and we will use the same variables as the link above so to make it easier to understand


var
  Students : array of string; 
  nCounter: integer;
begin
  // set the length to something
  SetLength(Students,5); // this will hold six students
  // loop through and load the array with some data
  for nCounter := Low(Students) to High(Students) do
  begin
    Students[nCounter] := 'My student #: ' + inttostr(nCounter);
  end;

  // do something
  // way one to free array
  Students := nil;
  // way two to free array
  SetLength(Students,0);
  // way three to free array
  Finalize(Students);
end;


There you go!

No comments:

Post a Comment