Create an object at a given address: Difference between revisions

→‎Tcl: Added implementation
(Refine the task slightly, crosslink)
(→‎Tcl: Added implementation)
Line 37:
456
</pre>
 
=={{header|Tcl}}==
As noted in the [[Address Operations]] task, it is highly unusual to work with low-level addresses in Tcl. However it is possible to use Tcl's [[C]] API (specifically <code>[http://www.tcl.tk/man/tcl8.6/TclLib/LinkVar.htm Tcl_LinkVar]</code>) to couple Tcl variables to a particular address:
 
{{libheader|critcl}}
<lang tcl>package require critcl
critcl::cproc linkvar {Tcl_Interp* interp char* var1} int {
int *intPtr = (int *) ckalloc(sizeof(int));
 
*intPtr = 0;
Tcl_LinkVar(interp, var1, (void *) intPtr, TCL_LINK_INT);
return (int) intPtr;
}
critcl::cproc linkagain(Tcl_Interp* interp int addr char* var2} void {
int *intPtr = (int *) addr;
Tcl_LinkVar(interp, var2, (void *) intPtr, TCL_LINK_INT);
}
package provide machAddrDemo 1</lang>
Demonstrating:
<lang tcl>package require machAddrDemo
set addr [linkvar foo]
puts "var 'foo' at $addr with value $foo"
linkagain $addr bar
puts "var 'bar' at $addr with value $bar"
incr foo
puts "incremented 'foo' so 'bar' is $bar"</lang>
Example output (your mileage may vary when it comes to addresses):
<pre>var 'foo' at 19363848 with value 0
var 'bar' at 19363848 with value 0
incremented 'foo' so 'bar' is 1</pre>
 
{{omit from|Java}}
Anonymous user