Pointer

From Free Pascal wiki
Jump to: navigation, search

Deutsch (de) English (en) français (fr)

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

Concept

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;
{$TYPEDADDRESS ON}
var 
  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
begin
  //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 
end.

Data Type

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:

{$TYPEDADRESS ON} 
Var 
  a : Integer = 20;
  b : Real = 20.5;
  c : Boolean = true;
  ptr : Pointer;
begin
  ptr := @a; // valid to hold Integer address 
  ptr := @b; // valid to hold Real address
  ptr := @c  // valid to hold Boolean address
end.

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:

Var 
  a      : Integer = 20;
  ptr    : Pointer;
begin
  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
end.

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:

Data Types
Simple Data Types Boolean | Byte | Cardinal | Char | Currency | Extended | Int64 | Integer | Longint | Pointer | Real | Shortint | Smallint | Word
Complex Data Types Array | Class | Record | Set | String