Difference between revisions of "Addr"

From Lazarus wiki
(application: qualify claim; allegedly insert new external links [spam detection false positive])
m (application: correct copy/writing mistake and allegedly insert new external links [spam detection false positive])
Line 33: Line 33:
  
 
Also, in [[Mode FPC|<syntaxhighlight lang="pascal" inline>{$mode FPC}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]] the <syntaxhighlight lang="pascal" inline>@</syntaxhighlight>‑address operator ''has'' to be used to assign values to procedural values (unless <syntaxhighlight lang="pascal" inline>{$modeSwitch classicalProcVars+}</syntaxhighlight> is set).
 
Also, in [[Mode FPC|<syntaxhighlight lang="pascal" inline>{$mode FPC}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]] the <syntaxhighlight lang="pascal" inline>@</syntaxhighlight>‑address operator ''has'' to be used to assign values to procedural values (unless <syntaxhighlight lang="pascal" inline>{$modeSwitch classicalProcVars+}</syntaxhighlight> is set).
In [[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]] and [[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]], however, no operator at all may be used.
+
In [[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode TP}</syntaxhighlight>]] and [[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]], however, no operator at all may be used.
  
 
Last but not least, in [[Asm|<syntaxhighlight lang="delphi" inline>asm</syntaxhighlight>]] blocks, <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> cannot be used to obtain addresses of labels, but <syntaxhighlight lang="pascal" inline>@</syntaxhighlight> can.
 
Last but not least, in [[Asm|<syntaxhighlight lang="delphi" inline>asm</syntaxhighlight>]] blocks, <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> cannot be used to obtain addresses of labels, but <syntaxhighlight lang="pascal" inline>@</syntaxhighlight> can.

Revision as of 09:02, 24 June 2020

Deutsch (de) English (en)

Addr is a compiler intrinsic behaving like a unary function returning the address of an object (if such exits). It is equivalent to the @ address operator, however, addr always returns an untyped pointer (since FPC 2.6.0). This behavior is compliant to Borland Pascal’s definition of the addr function, where this function originally comes from.

In PXSC, the loc function is the same as addr presented here.

usage

Addr is used just like every other function.

program addrDemo(input, output, stdErr);
var
	p: pointer;
begin
	p := addr(p);
	writeLn('Pointer p is located at ', sysBackTraceStr(p), '.');
end.

Addr can only be used on objects that have an address, that means reserve memory. The following objects do not have an address, thus addr cannot be used on them:

  • module identifiers: modules form namespaces, i. e. become part of identifier definitions within the modules
  • constant expressions: constants do not identify objects, meaning they do not occupy any memory
  • compiler intrinsics, such as writeLn or addr itself
  • (despite being a special case of functions) operator overloads
  • properties

In case of types, the typeInfo compiler intrinsic has to be used in order to obtain a reference to RTTI.

application

Addr primarily exists for compatibility with code that was written for/with Borland Pascal. The @‑address operator (in conjunction with {$typedAddress on}) should be preferred, since it can return typed addresses. This will prevent some programming mistakes.

Also, in {$mode FPC} and {$mode objFPC} the @‑address operator has to be used to assign values to procedural values (unless {$modeSwitch classicalProcVars+} is set). In {$mode TP} and {$mode Delphi}, however, no operator at all may be used.

Last but not least, in asm blocks, addr cannot be used to obtain addresses of labels, but @ can.