Difference between revisions of "Pointer"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Pointer}}
 
{{Pointer}}
  
'''Pointer''' has two meanings in Free Pascal:
+
'''Pointer''' has two meanings in [[FPC|Free Pascal]]:
*term or concept for a class of types and variables of those types
+
*term or concept for a class of types and [[Variable|variables]] of those types
*a simple data type that can hold the address of any data type
+
*a simple [[Data type|data type]] that can hold the address of any data type
 
   
 
   
 +
 
== Concept ==  
 
== 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.  
+
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|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.  
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
program test_pointer_wiki2;
 
program test_pointer_wiki2;
 
{$TYPEDADDRESS ON}
 
{$TYPEDADDRESS ON}
Line 32: Line 34:
  
 
== Data Type ==
 
== Data Type ==
 +
 +
 +
Back to [[Data type|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:
 
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>
+
<syntaxhighlight lang=pascal>
 
{$TYPEDADRESS ON}  
 
{$TYPEDADRESS ON}  
 
Var  
 
Var  
Line 44: Line 51:
 
   ptr := @a; // valid to hold Integer address  
 
   ptr := @a; // valid to hold Integer address  
 
   ptr := @b; // valid to hold Real address
 
   ptr := @b; // valid to hold Real address
   ptr := @c // valid to hold Boolean address
+
   ptr := @c; // valid to hold Boolean address
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 54: Line 61:
 
For example:
 
For example:
 
   
 
   
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
Var  
 
Var  
 
   a      : Integer = 20;
 
   a      : Integer = 20;
Line 70: Line 77:
 
== Predeclared Pointer Types ==
 
== Predeclared Pointer Types ==
 
The [[System unit]] of the standard [[RTL|Runtime Library]] declares a number of pointer types for the built-in variables including:
 
The [[System unit]] of the standard [[RTL|Runtime Library]] declares a number of pointer types for the built-in variables including:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
   PSmallInt          = ^Smallint;
 
   PSmallInt          = ^Smallint;
 
   PShortInt          = ^Shortint;
 
   PShortInt          = ^Shortint;
Line 84: Line 92:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   
 
   
 +
== See also ==
  
See also:
 
 
* Usage of [[Pointers]]
 
* Usage of [[Pointers]]
 
* http://www.freepascal.org/docs-html/ref/refse15.html
 
* http://www.freepascal.org/docs-html/ref/refse15.html
  
 
{{Data types}}
 
{{Data types}}

Revision as of 08:15, 23 February 2020

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


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

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:

{$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


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