Addr

From Free Pascal wiki
Jump to navigationJump to search

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