Difference between revisions of "@"
m (add Category:Code) |
(add Template:Symbols) |
||
Line 23: | Line 23: | ||
writeLn(foo); | writeLn(foo); | ||
end.</syntaxhighlight> | end.</syntaxhighlight> | ||
− | It | + | 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. |
With <syntaxhighlight lang="pascal" enclose="none">{$typedaddress on}</syntaxhighlight> compilation fails with an incompatible type error. | With <syntaxhighlight lang="pascal" enclose="none">{$typedaddress on}</syntaxhighlight> compilation fails with an incompatible type error. | ||
You usually want the latter behavior (compile-time failure) instead of wasting time with hours of debugging. | You usually want the latter behavior (compile-time failure) instead of wasting time with hours of debugging. | ||
Line 30: | Line 30: | ||
* [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" enclose="none">@</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" enclose="none">@</syntaxhighlight>)] | ||
+ | |||
+ | {{Symbols}} | ||
[[Category:Symbols]] | [[Category:Symbols]] | ||
[[Category:Code]] | [[Category:Code]] |
Revision as of 15:10, 24 March 2018
│
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
The address operator @
returns the address of a variable, procedure or function.
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 {$typedaddress on}
.
Here some example to demonstrate, what produces with untyped pointers valid and functional code, but semantically outputs an erroneous result:
0program untypedAddressDemo(input, output, stderr);
1
2procedure incrementIntByRef(const ref: PByte);
3begin
4 inc(ref^);
5end;
6
7var
8 foo: integer;
9begin
10 foo := -1;
11 incrementIntByRef(@foo);
12 writeLn(foo);
13end.
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 failure) instead of wasting time with hours of debugging.
read more
single characters |
|
character pairs |
|