[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2 absolute

Synopsis

 
var
  variable name: data type absolute variable reference;
or
 
var
  variable name: data type absolute integer expression;

Description

The first meaning of the `absolute' directive allows to put a variable to the address of another one and thus provides a type-casting mechanism.

In most cases, variable reference will be just a variable name, but GPC also allows arbitrary pointer expressions here. If variable reference has neither a constant address nor is a variable parameter, GPC prints a warning. This warning is suppressed in "extended syntax" mode which is switched on by the `--extended-syntax' option or the `{$X+}' compiler directive.

GPC also allows explicit type casts. Variant records (as defined in ISO-7185 Pascal), however, have no guaranteed overlaying and are therefore not suitable for type casts.

The second meaning of `absolute' places a variable at a specified address. This is useful on machines without virtual memory addressing for doing certain low-level operations, but should be avoided on systems with memory protection such as Unix-like systems. GPC does not check whether the specified virtual address makes any sense and does not provide a built-in mechanism to map it to a real address.

GPC warns about this second use of `absolute' unless "extended syntax" has been requested.

Conforming to

`absolute' is a Borland Pascal extension.

Borland Pascal has a slightly different syntax for the second meaning related to the addressing scheme of IA32 processors working in real mode.

Allowing arbitrary memory references instead of just variable names in the first meaning of `absolute' is a GNU Pascal extension.

Example

 
program AbsoluteDemo;

{$X+}

const IOMem = $f0000000;

var Mem: array [0 .. High (Cardinal)] of Byte absolute 0;

{ This address has no actual meaning } MyPort: Byte absolute IOMem + $c030;

{ Beware: Using any of the variables above will crash your program unless you know exactly what you do! That's why GPC warns about it without the $X+ directive. }

var x: Real; a: array [1 .. SizeOf (Real)] of Byte absolute x; i: Integer; b: Byte absolute a[i]; { GNU extension: non-constant memory reference. }

begin x := 3.14;

{ Look at the internal representation of a real variable. } for i := 1 to SizeOf (Real) do Write (a[i] : 4); WriteLn;

{ The same again, more ugly ... } for i := 1 to SizeOf (Real) do Write (b : 4); WriteLn;

{ And yes, there's an even more ugly way to do it ... } for i := 1 to SizeOf (Real) do Write (Mem[PtrCard (@x) + i - 1] : 4); WriteLn end.

See also

section 9.222 record, section 8.7 Type Casts.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Frank Heckenbach on May, 10 2002 using texi2html