Difference between revisions of "Addr"

From Lazarus wiki
Jump to navigationJump to search
(rewrite)
 
(7 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
This behavior is compliant to [[Borland Pascal|Borland Pascal’s]] definition of the <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> function, where this function originally comes from.
 
This behavior is compliant to [[Borland Pascal|Borland Pascal’s]] definition of the <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> function, where this function originally comes from.
  
In PXSC, the <syntaxhighlight lang="pascal" inline>loc</syntaxhighlight> function is the same as <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> presented here.
+
In [[PXSC|Pascal-XSC]], the <syntaxhighlight lang="pascal" inline>loc</syntaxhighlight> function is the same as <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> presented here.
  
 
== usage ==
 
== usage ==
Line 32: Line 32:
 
This will prevent some programming mistakes.
 
This will prevent some programming mistakes.
  
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.
+
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.
 +
 +
== see also ==
 +
* [[FPC New Features 3.2.0#FarAddr internal function|<syntaxhighlight lang="pascal" inline>farAddr</syntaxhighlight> (relevant for i8086-msdos platform)]]

Latest revision as of 13:33, 29 February 2024

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 Pascal-XSC, 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.

see also