Difference between revisions of "Addr"

From Lazarus wiki
(English translation of German page)
 
(add see also: farAddr; supposedly insert new external links [spam detection false positive])
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{LanguageBar}}
 
{{LanguageBar}}
  
== addr ==
+
{{Doc|package=RTL|unit=system|identifier=addr|text=<syntaxhighlight lang="pascal" inline>Addr</syntaxhighlight>}} is a compiler intrinsic behaving like a unary [[Function|function]] returning the address of an object (if such exits).
 +
It is equivalent to the [[@|<syntaxhighlight lang="pascal" inline>@</syntaxhighlight> address operator]], however, <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> always returns an ''untyped'' [[Pointer|pointer]] ([[User Changes 2.6.0#Result type of the addr() operator|since FPC 2.6.0]]).
 +
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.
  
Determines the address of a variable.
+
In PXSC, the <syntaxhighlight lang="pascal" inline>loc</syntaxhighlight> function is the same as <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> presented here.
  
Example:
+
== usage ==
 
+
<syntaxhighlight lang="pascal" inline>Addr</syntaxhighlight> is used just like every other function.
<syntaxhighlight lang="pascal">
+
<syntaxhighlight lang="pascal" highlight="5">
  var
+
program addrDemo(input, output, stdErr);
  i : integer;
+
var
  p : pointer;
+
p: pointer;
begin
+
begin
  i := 500;
+
p := addr(p);
  p := Addr(i);
+
writeLn('Pointer p is located at ', sysBackTraceStr(p), '.');
 
+
end.
  WriteLn(Integer(p^)) ;
 
  WriteLn('"a" is at the following address:', PtrInt(p));
 
end.
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<syntaxhighlight lang="pascal" inline>Addr</syntaxhighlight> can only be used on objects that have an address, that means reserve memory.
 +
The following objects do not have an address, thus <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> cannot be used on them:
 +
* module [[Identifier|identifiers]]: modules form [[Namespaces|namespaces]], i. e. become part of identifier definitions within the modules
 +
* constant expressions: [[Constant|constants]] do not identify objects, meaning they do not occupy any memory
 +
* compiler intrinsics, such as [[Write|<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight>]] or <syntaxhighlight lang="pascal" inline>addr</syntaxhighlight> itself
 +
* (despite being a special case of <syntaxhighlight lang="pascal" inline>function</syntaxhighlight>s) [[Operator overloading|operator overloads]]
 +
* [[Property|properties]]
 +
In case of [[Type|<syntaxhighlight lang="pascal" inline>type</syntaxhighlight>s]], the {{Doc|package=RTL|unit=system|identifier=typeinfo|text=<syntaxhighlight lang="pascal" inline>typeInfo</syntaxhighlight>}} compiler intrinsic has to be used in order to obtain a reference to [[RTTI]].
  
== Alternative ==
+
== application ==
 
+
<syntaxhighlight lang="pascal" inline>Addr</syntaxhighlight> primarily exists for compatibility with code that was written for/with Borland Pascal.
You can use @ instead of Addr(...).
+
The <syntaxhighlight lang="pascal" inline>@</syntaxhighlight>‑address operator (in conjunction with <syntaxhighlight lang="pascal" inline>{$typedAddress on}</syntaxhighlight>) should be preferred, since it can return ''typed'' addresses.
 
+
This will prevent some programming mistakes.
The two examples below do the same thing.
 
 
 
<syntaxhighlight lang="pascal">
 
  p := Addr(i);
 
  p := @i;
 
</syntaxhighlight>
 
 
 
Where @ can also be used to determine the addresses of procedures and functions.
 
 
 
== See also ==
 
 
 
* [[Pointer]]
 
  
{{Data_types}}
+
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 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.
  
[[Category:Pascal]]
+
== see also ==
[[Category:Data types]]
+
* [[FPC New Features 3.2.0#FarAddr internal function|<syntaxhighlight lang="pascal" inline>farAddr</syntaxhighlight> (relevant for i8086-msdos platform)]]

Latest revision as of 11:31, 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.

see also