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.

Answers


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:

SetLength(Data, 100);

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.


Need Your Help

Can't retreive value from ItemProprieties outlook : Operation failed

outlook outlook-2007 outlook-2010 outlook-vba outlook-2003

another problem that I can't figure out, once again. I'm retreiving a custom proprieties previously added by my code in an MailItem. I'm currently testing with outlook 2010, it's as straightforward...

puppet enterprise ssl cert error

ssl amazon-ec2 puppet puppet-enterprise

I'm trying to install puppet enterprise on EC2.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.