Address of a variable: Difference between revisions

Content deleted Content added
→‎Tcl: Added implementation
Line 170: Line 170:


In addition some folks have written binary Python modules which implement "peek" and "poke" operations, but these are non-standard.
In addition some folks have written binary Python modules which implement "peek" and "poke" operations, but these are non-standard.

=={{header|Tcl}}==
It is highly unusual to want to directly manipulate the address of a variable in Tcl, as it is a thoroughly unsafe operation. Indeed, Tcl does not expose any mechanism to do so at the script level. However, Tcl does contain a C-level API function, <tt>[http://www.tcl.tk/man/tcl8.6/TclLib/LinkVar.htm Tcl_LinkVar]</tt>, to arrange for a variable's value to always reflect the contents of a particular address in memory.

However, that's not the only way of doing it. You can also use the '''critcl''' library to put C code directly inside a Tcl script and so work with addresses directly that way.
<br>
{{libheader|critcl}}
<lang tcl>package require critcl
critcl::cproc peek {int addr} int {
union {
int i;
int *a;
} u;

u.i = addr;
return *u.a;
}
critcl::cproc poke {int addr int value} void {
union {
int i;
int *a;
} u;

u.i = addr;
*u.a = value;
}
package provide poker 1.0</lang>
Have great care with this sort of code; the damage you can do by writing to random locations is considerable and being able to read from anywhere could allow information to flow to otherwise unauthorized programs.


=={{header|Toka}}==
=={{header|Toka}}==
Line 208: Line 236:
{{Omit From|Java}}
{{Omit From|Java}}
{{Omit From|Metafont}}
{{Omit From|Metafont}}
{{Omit From|Tcl}}