Difference between revisions of "Pointer"

From Lazarus wiki
Jump to navigationJump to search
m (Or object)
(Separate general concept and data type)
Line 1: Line 1:
 
{{Pointer}}
 
{{Pointer}}
  
A '''pointer''' is a data type which contains a [[reference]]/pointer to the memory address of the value, complex object or [[variable]] it points to.
+
'''Pointer''' has two meanings in Free Pascal:
 +
*term or concept
 +
*a simple data type that can hold the address of any data type
 +
 +
== Concept ==
 +
A '''pointer''' is a simple data type which 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 the ^ operator in front of the data type. A pointer type can only hold the addresses of variables or values of the specific type the pointer type has been declared to point to.  
  
Pointers can be declared in several ways:
+
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.
p: Pointer;  // direct use of pointer-type
+
 
  pL: ^Longint; // usage of pointer-to-type notation.
+
<syntaxhighlight>
 +
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.
 +
</syntaxhighlight>
 +
 
 +
 
 +
 
 +
== 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:
 +
 
 +
<syntaxhighlight>
 +
{$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.
 +
</syntaxhighlight>
 +
 
 +
But, before a Pointer variable with a value can be used, it must be
 +
#cast to the type of the value or variable that it points to
 +
#de-referenced with the '''^''' operator
 +
 
 +
For example:
 +
   
 +
<syntaxhighlight>
 +
Var
 +
  a      : Integer = 20;
 +
  ptr    : Pointer;
 +
  pInt  : PInteger; // PInteger is predeclared in the RTI
 +
  ptrInt : ^Integer; // ^ in front of a type declares a type of "pointer to specified type"
 +
                   
 +
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 := @b; // valid to hold Real address
 +
  ptr := @c  // valid to hold Boolean address
 +
end.
 +
</syntaxhighlight>
 +
 
 +
== Predeclared Pointer Types ==
 +
The [[System unit]] of the standard [[RTL|Runtime Library]] declares a number of pointer types for the built-in variables including:
 +
<syntaxhighlight>
 +
  PSmallInt          = ^Smallint;
 +
  PShortInt          = ^Shortint;
 +
  PInteger            = ^Integer;
 +
  PByte              = ^Byte;
 +
  PWord              = ^word;
 +
  PDWord              = ^DWord;
 +
  PLongWord          = ^LongWord;
 +
  PLongint            = ^Longint;
 +
  PCardinal          = ^Cardinal;
 +
  PQWord              = ^QWord;
 +
  PBoolean            = ^Boolean;
 +
</syntaxhighlight>
 +
  
 
See also:
 
See also:

Revision as of 14:57, 19 July 2016

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

Pointer has two meanings in Free Pascal:

  • term or concept
  • a simple data type that can hold the address of any data type

Concept

A pointer is a simple data type which 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 the ^ operator in front of the data type. A pointer type can only hold the addresses of variables or values of the specific type the pointer type has 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;
  pInt   : PInteger; // PInteger is predeclared in the RTI
  ptrInt : ^Integer; // ^ in front of a type declares a type of "pointer to specified type"
                     
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 := @b; // valid to hold Real address
  ptr := @c  // valid to hold Boolean address
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:


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