From Free Pascal wiki
Revision as of 07:15, 23 February 2020 by Trev (talk | contribs) (Fixed syntax highlighting)
Jump to navigationJump to search

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

Pointer has two meanings in Free Pascal:

  • term or concept for a class of types and variables of those types
  • a simple data type that can hold the address of any data type


A pointer is a variable that contains the memory address of a local or global variable, or the memory address of a value or complex object that has been created and stored in the heap part of a program's memory layout. It can be said to be a reference for the value/variable, or that it "points to" the value/variable. For any data type, a pointer type for that data can be declared using operator ^ in front of the data type. With the {$TYPEDADDRESS ON} compiler directive set (advisable), pointers can only hold the addresses of variables or values of the same type that they have been declared to point to.

Pointer variables are assigned the address of a variable with the @ operator and they are assigned the address of a value/object on the heap by using the output of the New procedure. To access, for reading or writing, the data "pointed to" by a pointer variable, you de-reference it with the ^ operator.

program test_pointer_wiki2;
  a         : Integer = 100;
  b         : Real = 10.5;
  ptrToInt  : PInteger;  // PInteger is a pointer type declared in the RTL
                         // which can only point to Integer value/variab
  ptrToInt2 : ^Integer;  // the general format for declaring a pointer type
  //ptrToInt := @b;      // compile error with {$TYPEDADDRESS ON} 
  ptrToInt := @a;        
  //Writeln(ptrToInt);   // compiler error: Error: Can't read or write variables of this type
  Writeln(ptrToInt^);    // prints 100
  New(ptrToInt2);        // allocate an un-initialized Integer on the heap
  Writeln(ptrToInt2^);   // writes random integer value
  ptrToInt2^ := 222;     // assign a value to the heap allocated integer
  Writeln(ptrToInt2^);   // prints 222 

Data Type

Back to data types.

The Pointer data type is a simple type which can contain the address of a variable or value of any data type. This is in contrast to typed pointer variables (e.g. PInteger) that can only point to a variables or values of a specific type with compiler directive {$TYPEDADDRESS ON} . For example:

  a : Integer = 20;
  b : Real = 20.5;
  c : Boolean = true;
  ptr : Pointer;
  ptr := @a; // valid to hold Integer address 
  ptr := @b; // valid to hold Real address
  ptr := @c; // valid to hold Boolean address

But, before a Pointer variable with a value can be used, it must be

  1. cast to the type of the value or variable that it points to
  2. de-referenced with the ^ operator

For example:

  a      : Integer = 20;
  ptr    : Pointer;
  ptr := @a;          // valid to hold Integer address 
  //Writeln(ptr);     // Invalid - will not compile
                      // Error: Can't read or write variables of this type
  //Writeln( ptr^);   // Error: Can't read or write variables of this type
                      // Just de-referencing a Pointer variable is not sufficient
  Writeln( PInteger(ptr)^); // After cast, then de-reference: prints 20

Predeclared Pointer Types

The System unit of the standard Runtime Library declares a number of pointer types for the built-in variables including:

  PSmallInt           = ^Smallint;
  PShortInt           = ^Shortint;
  PInteger            = ^Integer;
  PByte               = ^Byte;
  PWord               = ^word;
  PDWord              = ^DWord;
  PLongWord           = ^LongWord;
  PLongint            = ^Longint;
  PCardinal           = ^Cardinal;
  PQWord              = ^QWord;
  PBoolean            = ^Boolean;

See also

navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring