4.4 Initialized variables

By default, simple variables in Pascal are not initialized after their declaration. Any assumption that they contain zero or any other default value is erroneous: They can contain rubbish. To remedy this, the concept of initialized variables exists. The difference with normal variables is that their declaration includes an initial value, as can be seen in the diagram in the previous section.

Remark Two exceptions to this rule exist:

  1. Managed types are an exception to this rule: Managed types are always initialized with a default value: in general this means setting the reference count to zero, or setting the pointer value of the type to Nil. See section 3.9, page 243

  2. Global variables are initialized with the equivalent of zero.

Note that the behavior of zeroing out certain variables can result in invalid content for variables:

Type
  TWeekDays =
    (monday,tuesday,wednesday,thursday,friday,saturday,sunday);
  TWeekend = saturday..sunday;

var
  W : TWeekend;

begin
  Writeln(W);
end.

The above will result, when run, in an error:

Runtime error 107 at $000000000040024A
$000000000040024A
$000000000042BF70
$00000000004001D2

Therefore it is Highly recommended to always initialize variables before using them.

This can be easily done in the declaration of the variables. Given the declaration:

Var
  S : String = 'This is an initialized string';

The value of the variable following will be initialized with the provided value. The following is an even better way of doing this:

Const
  SDefault = 'This is an initialized string';

Var
  S : String = SDefault;

Initialization is often used to initialize arrays and records. For arrays, the initialized elements must be specified, surrounded by round brackets, and separated by commas. The number of initialized elements must be exactly the same as the number of elements in the declaration of the type. As an example:

Var
  tt : array [1..3] of string[20] = ('ikke', 'gij', 'hij');
  ti : array [1..3] of Longint = (1,2,3);

For constant records, each element of the record that you wish to initialize must be specified in the form Field: Value, separated by semicolons, and surrounded by round brackets.. You can omit fields that you don’t wish to initialize, in fact you can skip all fields. If you skip fields, the compiler will emit a warning.

As an example:

Type
  Point = record
    X,Y : Real
    end;
Var
  Origin : Point = (X:0.0; Y:0.0);
  Partial : Point = (X:0.0);
  Empty : Point = ();

The above declarations will result in the following warnings:

iv.pp(7,27) Warning: Some fields coming after "X" were not initialized
iv.pp(8,20) Warning: Some fields coming after "" were not initialized

The order of the fields in a constant record needs to be the same as in the type declaration, otherwise a compile-time error will occur.

Remark It should be stressed that initialized variables are initialized when they come into scope, in difference with typed constants, which are initialized at program start. This is also true for local initialized variables. Local initialized variables are initialized whenever the routine is called. Any changes that occurred in the previous invocation of the routine will be undone, because they are again initialized.

Remark Care should be taken when using initialized pointer types such as PChars. In the following examples, S is a pointer, pointing to a block of constant (read-only) program data. Assigning a character in the string will therefore not work. Assigning S itself will of course work. The first routine will give an error, the second not:

procedure foo1;
var
  s: PChar = 'PChar';
begin
  s[0] := 'a';
end;

procedure foo2;
var
  s: PChar;
begin
  s := 'PChar';
  s[0] := 'a';
end;