How can I reserve memory for pointer to an array in Delphi?
I'm developing class to represent special kind of matrix:
type DifRecord = record Field: String; Number: Byte; Value: smallint; end; type TData = array of array of MainModule.DataRecord; type TDifference = array of DifRecord; type TFogelMatrix = class private M: Byte; N: Byte; Data: ^TData; DifVector: ^TDifference; procedure init(); public constructor Create(Rows, Cols: Byte); destructor Destroy; end;
Now in constructor I need to reserve memory for Data and DifVector class members. I use pointers to array of records, as you see. So, the main question is, how can I correctly reserve memory? I suppose I can not use something like that: new(Data); new(DifVector); cause I`m loosing the main idea - to reserve memory space, as much as I want to, at run-time. Thanks for comments.
Since you're using dynamic arrays, array of, then you should use SetLength to specify the length of the array, which can be done dynamically.
ie. like this:
This will not reserve 100 bytes, but will reserve enough space to hold 100 elements of whatever type the array holds.
Change your declarations for the arrays to simple arrays:
Data: TData; DifVector: TDifference;
and use it with SetLength, it should do the trick.