Addr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Addr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [`Addr`](https://www.freepascal.org/docs-html/rtl/system/addr.html) is a compiler intrinsic behaving like a unary [[Function|function]] returning the address of an object (if such exits). It is equivalent to the [[@|@ address operator]], however, `addr` always returns an *untyped* [[Pointer|pointer]] ([[User Changes 2.6.0|since FPC 2.6.0]]). This behavior is compliant to [[Borland Pascal|Borland Pascal’s]] definition of the `addr` function, where this function originally comes from. | |
| 2 | + | ||
| 3 | + | In [[PXSC|Pascal-XSC]], the `loc` function is the same as `addr` presented here. | |
| 4 | + | ||
| 5 | + | ## usage | |
| 6 | + | ||
| 7 | + | `Addr` is used just like every other function. | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | program addrDemo(input, output, stdErr); | |
| 11 | + | var | |
| 12 | + | p: pointer; | |
| 13 | + | begin | |
| 14 | + | p := addr(p); | |
| 15 | + | writeLn('Pointer p is located at ', sysBackTraceStr(p), '.'); | |
| 16 | + | end. | |
| 17 | + | ``` | |
| 18 | + | ||
| 19 | + | `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: | |
| 20 | + | ||
| 21 | + | - module [[Identifier|identifiers]]: modules form [[Namespaces|namespaces]], i. e. become part of identifier definitions within the modules | |
| 22 | + | - constant expressions: [[Constant|constants]] do not identify objects, meaning they do not occupy any memory | |
| 23 | + | - compiler intrinsics, such as [[Write|writeLn]] or `addr` itself | |
| 24 | + | - (despite being a special case of `function`s) [[Operator overloading|operator overloads]] | |
| 25 | + | - [[Property|properties]] | |
| 26 | + | ||
| 27 | + | In case of [[Type|types]], the [`typeInfo`](https://www.freepascal.org/docs-html/rtl/system/typeinfo.html) compiler intrinsic has to be used in order to obtain a reference to [[RTTI]]. | |
| 28 | + | ||
| 29 | + | ## application | |
| 30 | + | ||
| 31 | + | `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. | |
| 32 | + | ||
| 33 | + | Also, in [[Mode FPC|{$mode FPC}]] and [[Mode ObjFPC|{$mode objFPC}]] the `@`‑address operator *has* to be used to assign values to procedural values (unless `{$modeSwitch classicalProcVars+}` is set). In [[Mode TP|{$mode TP}]] and [[Mode TP|{$mode Delphi}]], however, no operator at all may be used. | |
| 34 | + | ||
| 35 | + | Last but not least, in [[Asm|asm]] blocks, `addr` cannot be used to obtain addresses of labels, but `@` can. | |
| 36 | + | ||
| 37 | + | ## see also | |
| 38 | + | ||
| 39 | + | - [[FPC New Features 3.2.0|farAddr (relevant for i8086-msdos platform)]] | |
| 40 | + | ||
| 41 | + | --- | |
| 42 | + | *From the Free Pascal wiki, page "Addr", as it stood on 2024-02-29 after a change by Chernov, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
