Difference between revisions of "@"

From Free Pascal wiki
(read more: replace link to doc by wiki page link)
(typo; replace legacy syntaxhighlight syntax; mention loc function of PXSC; unify code style; more specific link target compile-time error)
 
Line 3: Line 3:
 
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">@</div>
 
<div style="float:left; margin: 0 25px 20px 0; padding:50px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">@</div>
  
The address operator <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> returns the address of an [[Identifier|identifier]] that is associated with an address (usually a [[Variable|variable]] or [[Routine|routine]], but also a [[Label#assembler|label]]).
+
The address operator <syntaxhighlight lang="pascal" inline>@</syntaxhighlight> returns the address of an [[Identifier|identifier]] that is associated with an address (usually a [[Variable|variable]] or [[Routine|routine]], but also a [[Label#assembler|label]]).
  
Normally, the value <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> returns is an ''untyped'' [[Pointer|<syntaxhighlight lang="pascal" enclose="none">pointer</syntaxhighlight>]].
+
Normally, the value <syntaxhighlight lang="pascal" inline>@</syntaxhighlight> returns is an ''untyped'' [[Pointer|<syntaxhighlight lang="pascal" inline>pointer</syntaxhighlight>]].
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|<syntaxhighlight lang="pascal" enclose="none">{$typedaddress on}</syntaxhighlight>]].
+
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|<syntaxhighlight lang="pascal" inline>{$typedAddres on}</syntaxhighlight>]].
  
Here some example to demonstrate, what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result:
+
Here is an example to demonstrate what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result:
 
<syntaxhighlight lang="pascal" line highlight="12">program untypedAddressDemo(input, output, stderr);
 
<syntaxhighlight lang="pascal" line highlight="12">program untypedAddressDemo(input, output, stderr);
  
Line 23: Line 23:
 
writeLn(foo);
 
writeLn(foo);
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
It was intended, that <syntaxhighlight lang="pascal" enclose="none">0</syntaxhighlight> (zero) gets printed, but the program prints <syntaxhighlight lang="pascal" enclose="none">-256</syntaxhighlight> instead.
+
It was intended that <syntaxhighlight lang="pascal" inline>0</syntaxhighlight>&nbsp;(zero) gets printed, but the program prints <syntaxhighlight lang="pascal" inline>-256</syntaxhighlight> instead.
With <syntaxhighlight lang="pascal" enclose="none">{$typedaddress on}</syntaxhighlight> compilation fails with an incompatible type error.
+
With <syntaxhighlight lang="pascal" inline>{$typedAddress on}</syntaxhighlight> compilation fails with an incompatible type error.
You usually want the latter behavior ([[Compile_time|compile-time]] failure) instead of wasting time with hours of debugging.
+
You usually want the latter behavior ([[compile-time error]]) instead of wasting time with hours of debugging.
  
 
== other remarks ==
 
== other remarks ==
* In [[ASCII]] the character <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> (AT sign): has the value <syntaxhighlight lang="pascal" enclose="none">64</syntaxhighlight>.
+
* In [[ASCII]] the character <syntaxhighlight lang="pascal" inline>@</syntaxhighlight> (AT sign): has the value <syntaxhighlight lang="pascal" inline>64</syntaxhighlight>.
 +
* <abbr title="Pascal extension for scientific computing">PXSC</abbr> defines the <syntaxhighlight lang="pascal" inline>loc</syntaxhighlight> function as the address-operator.
 +
 
 
== read more ==
 
== read more ==
* [https://www.freepascal.org/docs-html/ref/refse83.html The <syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight> operator]
+
* [https://www.freepascal.org/docs-html/ref/refse83.html The <syntaxhighlight lang="pascal" inline>@</syntaxhighlight> operator]
* [https://www.freepascal.org/docs-html/prog/progsu75.html Typed address operator (<syntaxhighlight lang="pascal" enclose="none">@</syntaxhighlight>)]
+
* [https://www.freepascal.org/docs-html/prog/progsu75.html Typed address operator (<syntaxhighlight lang="pascal" inline>@</syntaxhighlight>)]
* [[Addr|<syntaxhighlight lang="pascal" enclose="none">addr</syntaxhighlight>]]
+
* [[Addr|<syntaxhighlight lang="pascal" inline>addr</syntaxhighlight>]]
  
 
{{Symbols}}
 
{{Symbols}}

Latest revision as of 17:45, 9 July 2020

English (en) suomi (fi) français (fr) русский (ru)

@

The address operator @ returns the address of an identifier that is associated with an address (usually a variable or routine, but also a label).

Normally, the value @ returns is an untyped 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 directive {$typedAddres on}.

Here is an example to demonstrate what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result:

 1 program untypedAddressDemo(input, output, stderr);
 2 
 3 procedure incrementIntByRef(const ref: PByte);
 4 begin
 5 	inc(ref^);
 6 end;
 7 
 8 var
 9 	foo: integer;
10 begin
11 	foo := -1;
12 	incrementIntByRef(@foo);
13 	writeLn(foo);
14 end.

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.

other remarks

  • In ASCII the character @ (AT sign): has the value 64.
  • PXSC defines the loc function as the address-operator.

read more


navigation bar: topic: Pascal symbols
single characters

+ (plus)  •  - (minus)  •  * (asterisk)  •  / (slash)
= (equal)  •  > (greater than)  •  < (less than)
. (period)  •  : (colon)  •  ; (semi colon)
^ (hat)  •  @ (at)
$ (dollar sign)  •  & (ampersand)  •  # (hash)
' (single quote)

character pairs

<> (not equal)  •  <= (less than or equal)  •  := (becomes)  •  >= (greater than or equal)

 •  >< (symmetric difference)  •  // (double slash)