I am using a multi-dimensional dynamic array in delphi and am trying to figure this out:
I have 2 seperate values for the first index and second index that are totally seperate of each other.
As new values come I want to grow the array if that new value is outside of either bound.
For new values x, y
I check:
if Length(List) < (x + 1) then SetLength(List, x + 1); if Length(List[0]) < (y + 1) then SetLength(List, Length(List), y + 1);
Is this the correct way to do this or is there a better way to grow the array as needed?
I think you forgot to use the second index on the second dimension;
Your code should probably read like this :
Note the use of ‘x’ as the first dimension index when growing the second dimension.
One caution though :
You should be aware of the fact that Delphi uses reference-counting on dynamic arrays too (just like how it’s done with AnsiString). Because of this, growing the array like above will work, but any other reference to it will still have the old copy of it!
The only way around this, is keeping track of these array’s with one extra level of indirection – ie. : Use a pointer to the dynamic array (which is also a pointer in itself, but that’s okay).
Also note that any of those ‘external’ pointers should be updated in any situation that the address of the dynamic array could change, as when growing/shrinking it using SetLength().