@: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | 1 | @ | |
| 4 | - | ||
| 5 | - | ||
| 6 | 2 | ||
| 7 | 3 | The address operator `@` returns the address of an [[Identifier|identifier]] that is associated with an address (usually a [[Variable|variable]] or [[Routine|routine]], but also a [[Label|label]]). | |
| 8 | 4 | ||
| 9 | 5 | Normally, the value `@` returns is an *untyped* [[Pointer|pointer]]. If you are handling pointers a lot, and want to mitigate issues with passing references of wrong type’s target, you have use the [[Compiler directive|directive]] [[$typedAddress|{$typedAddress on}]]. | |
| 10 | 6 | ||
| 11 | 7 | Here is an example to demonstrate what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result: | |
| 12 | 8 | ||
| 13 | 9 | ``` pascal | |
| 14 | 10 | program untypedAddressDemo(input, output, stderr); | |
| 15 | 11 | ||
| 16 | 12 | procedure incrementIntByRef(const ref: PByte); | |
| 17 | 13 | begin | |
| 18 | 14 | inc(ref^); | |
| 19 | 15 | end; | |
| 20 | 16 | ||
| 21 | 17 | var | |
| 22 | 18 | foo: integer; | |
| 23 | 19 | begin | |
| 24 | 20 | foo := -1; | |
| 25 | 21 | incrementIntByRef(@foo); | |
| 26 | 22 | writeLn(foo); | |
| 27 | 23 | end. | |
| 28 | 24 | ``` | |
| 29 | 25 | ||
| 30 | 26 | It was intended that `0` (zero) gets printed, but the program prints `-256` instead. With `{$typedAddress on}` compilation fails with an incompatible type error. You usually want the latter behavior ([[compile-time error]]) instead of wasting time with hours of debugging. | |
| 31 | 27 | ||
| 32 | 28 | ## other remarks | |
| 33 | 29 | ||
| 34 | 30 | - In [[ASCII]] the character `@` (AT sign): has the value `64`. | |
| 35 | 31 | - [[PXSC]] defines the `loc` function as the address-operator. | |
| 36 | 32 | ||
| 37 | 33 | ## read more | |
| 38 | 34 | ||
| 39 | 35 | - [The `@` operator](https://www.freepascal.org/docs-html/ref/refse87.html) | |
| 40 | 36 | - [Typed address operator (`@`)](https://www.freepascal.org/docs-html/prog/progsu75.html) | |
| 41 | 37 | - [[Addr|addr]] | |
| 42 | 38 | ||
| 43 | 39 | --- | |
| 44 | 40 | *From the Free Pascal wiki, page "@", as it stood on 2024-02-28 after a change by Alextp, 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).* |
