What does it take to complete a multidimensional dynamic array?

I am using dynamic arrays and have no problem with the SetLength and Finalize procedures.

I recently had to use dynamic arrays where each element of the array could contain a variable number of elements. The declaration is like this:

TScheduleArray =  array of array of array [1..DaysPerWeek] of TShiftType;

      

The software works great, I have no problem with how to use this framework. You call SetLength on the main array and then you can call SetLength again on each element in the array. This works as expected.

SetLength(MyArray, 1);
SetLength(MyArray[0], 2);

      

My question is this, when I came to free up the resources used for this array, I just call Finalize on the array variable:

Finalize(MyArray);

      

or should each element of the array be terminated as well, since each element is a dynamic array itself?

+2


source to share


3 answers


Arrays are managed by the compiler and do not need any modification. If TShiftType is a class, you will have to deallocate objects manually, one at a time, but the array itself will be deleted if it goes out of scope.



+5


source


Quote : "You call SetLength on the main array and then you can call SetLength again every element of the array."

You don't need to iterate over your arrays.

SetLength()

takes a list of lengths for each dimension.

Example:



SetLength(ScheduleArray,200,15,35);

      

Same as:

SetLength(ScheduleArray,200);
for i:=low(ScheduleArray) to high(ScheduleArry) do
begin
  SetLength(ScheduleArray[i],15);
  for j:=low(ScheduleArray[i]) to high(ScheduleArray[i]) do
    SetLength(ScheduleArray[i,j],35);
end;

      

+6


source


To free any dynamic array, just assign it to zero for example:

a:array of array of integer;

      

To free it, use:

a:=nil;  

      

+1


source







All Articles