Skip to content

Array management by pointer

Form-Employers
Form Employers

In previous article Array management by value we talked about one way to grant to us access to array's items: we used a by value approach, in the sense we surely took copy of items values by dereferencing a pointer to record we defined at the top of code.
Our intention was to emphasize that a copy by value can even be made with the help of a pointer.
Pointers in that context could represent an alternative way to archive or reach the documents: instead of alphabetically (array[index]), by coordinates (for example, just like in battleship).

 

Result is essentially the same:

  • we compile an original document and copy it to our file in archive;
  • we fetch documents, then copy info through pointers, manipulate them.

In other words, we prepare a new record with all data, then we store these values into an array of records; so we pass values.
On the other side we create locally a new record, where to host all fetched data from array, with pointer's dereference.

We here propose the other face of the medal, by starting to define an array not of records, but of pointers to record:

 

type
  pEmployee = ^TEmployee; //Define a pointer of TEmployee

  TEmployee = Record
    Id : integer;
    Name  : string;
    Surname  : string;
  end;

type

  { TForm1 }

  TForm1 = class(TForm)
  ...
  private
    { private declarations }
    EmployeeArray  : Array of pEmployee; // Dynamic array of ^TEmployee
  ...
  end;

What happens now?

In this way the array, the only one we declare, contains pointers to record: but where are the records?

They still are in memory!