Marco's Web Center

Menu for Books
Delphi 2007 Handbook
Mastering Delphi 2005
Essential Delphi 8 for .NET
Mastering Delphi 7
Essential Pascal
Essential Delphi
Buy Books Online
Marco's TechBookStore

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old) Conferences

My Other Sites
Italian Site (
the delphi search

Spirit of delphi

Home My Blog Books Object Pascal Marco
Essential Pascal Cover

The cover of the 4th edition of Essential Pascal, the first available in print (and PDF) on

Marco Cantù's
Essential Pascal

Chapter 8

Author's Note: This chapter will cover memory handling, discuss the various memory areas, and introduce dynamic arrays. Temporarily only this last part is available.

Delphi 4 Dynamic Arrays

Traditionally, the Pascal language has always had fixed-size arrays. When you declare a data type using the array construct, you have to specify the number of elements of the array. As expert programmers probably know, there were a few techniques you could use to implement dynamic arrays, typically using pointers and manually allocating and freeing the required memory.

Delphi 4 introduces a very simple implementation of dynamic arrays, modeling them after the dynamic long string type I've just covered. As long strings, dynamic arrays are dynamically allocated and reference counted, but they do not offer a copy-on-write technique. That's not a big problem, as you can deallocate an array by setting its variable to nil.

You can now simply declare an array without specifying the number of elements and then allocate it with a given size using the SetLength procedure. The same procedure can also be used to resize an array without losing its content. There are also other string-oriented procedures, such as the Copy function, that you can use on arrays.

Here is a small code excerpt, underscoring the fact that you must both declare and allocate memory for the array before you can start using it:

procedure TForm1.Button1Click(Sender: TObject);
  Array1: array of Integer;
  Array1 [1] := 100; // error
  SetLength (Array1, 100);
  Array1 [99] := 100; // OK

As you indicate only the number of elements of the array, the index invariably starts from 0. Generic arrays in Pascal account for a non-zero low bound and for non-integer indexes, two features that dynamic arrays don't support. To learn the status of a dynamic array, you can use the Length, High, and Low functions, as with any other array. For dynamic arrays, however, Low always returns 0, and High always returns the length minus one. This implies that for an empty array High returns -1 (which, when you think about it, is a strange value, as it is lower than that returned by Low).

Figure 8.1: The form of the DynArr example

After this short introduction I can show you a simple example, called DynArr and shown in Figure 8.1. It is indeed simple because there is nothing very complex about dynamic arrays. I'll also use it to show a few possible errors programmers might make. The program declares two global arrays and initializes the first in the OnCreate handler:

  Array1, Array2: array of Integer;

procedure TForm1.FormCreate(Sender: TObject);
  // allocate
  SetLength (Array1, 100);

This sets all the values to zero. This initialization code makes it possible to start reading and writing values of the array right away, without any fear of memory errors. (Assuming, of course, that you don't try to access items beyond the upper bound of the array.) For an even better initialization, the program has a button that writes into each cell of the array:

procedure TForm1.btnFillClick(Sender: TObject);
  I: Integer;
  for I := Low (Array1) to High (Array1) do
    Array1 [I] := I;

The Grow button allows you to modify the size of the array without losing its contents. You can test this by using the Get value button after pressing the Grow button:

procedure TForm1.btnGrowClick(Sender: TObject);
  // grow keeping existing values
  SetLength (Array1, 200);

procedure TForm1.btnGetClick(Sender: TObject);
  // extract
  Caption := IntToStr (Array1 [99]);

The only slightly complex code is in the OnClick event of the Alias button. The program copies one array to the other one with the := operator, effectively creating an alias (a new variable referring to the same array in memory). At this point, however, if you modify one of the arrays, the other is affected as well, as they both refer to the same memory area:

procedure TForm1.btnAliasClick(Sender: TObject);
  // alias
  Array2 := Array1;
  // change one (both change)
  Array2 [99] := 1000;
  // show the other
  Caption := IntToStr (Array1 [99]);

The btnAliasClick method does two more operations. The first is an equality test on the arrays. This tests not the actual elements of the structures but rather the memory areas the arrays refer to, checking whether the variables are two aliases of the same array in memory:

procedure TForm1.btnAliasClick(Sender: TObject);
  if Array1 = Array2 then
  // truncate first array
  Array1 := Copy (Array2, 0, 10);

The second is a call to the Copy function, which not only moves data from one array to the other, but also replaces the first array with a new one created by the function. The effect is that the Array1 variable now refers to an array of 11 elements, so that pressing the Get value or Set value buttons produces a memory error and raises an exception (unless you have range-checking turned off, in which case the error remains but the exception is not displayed). The code of the Fill button continues to work fine even after this change, as the items of the array to modify are determined using its current bounds.


This chapter temporarily covers only dynamic arrays, certainly an important element for memory management, but only a portion of the entire picture. More material will follow.

The memory structure described in this chapter is typical of Windows programming, a topic I'll introduce in the next chapter (without going to the full extent of using the VCL, though).

Next Chapter: Windows Programming