@: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: @
| Old | New | Line | |
|---|---|---|---|
| 1 | + | <div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;"> | |
| 2 | + | ||
| 3 | + | @ | |
| 4 | + | ||
| 5 | + | </div> | |
| 6 | + | ||
| 7 | + | 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 | + | ||
| 9 | + | 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 | + | ||
| 11 | + | Here is an example to demonstrate what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result: | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | program untypedAddressDemo(input, output, stderr); | |
| 15 | + | ||
| 16 | + | procedure incrementIntByRef(const ref: PByte); | |
| 17 | + | begin | |
| 18 | + | inc(ref^); | |
| 19 | + | end; | |
| 20 | + | ||
| 21 | + | var | |
| 22 | + | foo: integer; | |
| 23 | + | begin | |
| 24 | + | foo := -1; | |
| 25 | + | incrementIntByRef(@foo); | |
| 26 | + | writeLn(foo); | |
| 27 | + | end. | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | 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 | + | ||
| 32 | + | ## other remarks | |
| 33 | + | ||
| 34 | + | - In [[ASCII]] the character `@` (AT sign): has the value `64`. | |
| 35 | + | - [[PXSC]] defines the `loc` function as the address-operator. | |
| 36 | + | ||
| 37 | + | ## read more | |
| 38 | + | ||
| 39 | + | - [The `@` operator](https://www.freepascal.org/docs-html/ref/refse87.html) | |
| 40 | + | - [Typed address operator (`@`)](https://www.freepascal.org/docs-html/prog/progsu75.html) | |
| 41 | + | - [[Addr|addr]] | |
| 42 | + | ||
| 43 | + | --- | |
| 44 | + | *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).* |
