Create an object at a given address: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 360:
 
=={{header|COBOL}}==
{{works with|COBOL| 2002}}
{{trans|PicoLisp}}
<syntaxhighlight lang="cobolcobolfree"> IDENTIFICATION DIVISION.
{{works with|COBOL|2002}}
PROGRAM-ID. object-address-test.
{{works with|OpenCOBOL|1.1}}
DATA DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
LOCAL-STORAGE SECTION.
PROGRAM-ID. object-address-test.
77 int-space PICTURE IS 9(5) VALUE IS 12345.
DATA DIVISION.
77 addr PICTURE 05 val PICTUREIS 9(5) BASED VALUE IS ZERO.
LOCAL-STORAGE SECTION.
77 point 01 int-space USAGE IS POINTER.
PROCEDURE DIVISION.
05 val PICTURE 9(5) VALUE 12345.
DISPLAY "Value of integer object : " int-space
01 addr BASED.
SET point TO ADDRESS OF int-space
05 val PICTURE 9(5) VALUE ZERO.
DISPLAY "Machine address of object : " point
01 point USAGE POINTER.
SET ADDRESS OF PROCEDUREaddr DIVISION.TO point
DISPLAY "Value of referent object : " addr
DISPLAY val OF int-space END-DISPLAY
MOVE SET point65535 TO ADDRESS OF int-space
DISPLAY "New value of original : " addr
DISPLAY point END-DISPLAY
DISPLAY "New value of reference SET ADDRESS OF addr: TO" pointint-space
GOBACK.
DISPLAY val OF addr END-DISPLAY
END PROGRAM object-address-test.</syntaxhighlight>
MOVE 65535 TO val OF addr
DISPLAY val OF addr END-DISPLAY
DISPLAY val OF int-space END-DISPLAY
STOP RUN.
END PROGRAM object-address-test.
</syntaxhighlight>
 
Output:
<pre>
Value of integer object : 12345
12345
Machine address of object : 0x0000563e11e77fd0
3215227472
Value of referent object : 12345
12345
New value of original : 65535
65535
New value of reference : 65535
65535
</pre>
 
=={{header|Commodore BASIC}}==
The <code>PEEK</code> and <code>POKE</code> commands allow the [[Commodore BASIC]] user to perform limited [[6502 Assembly]] operations.
Line 534 ⟶ 530:
Teresa 60
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
 
local fn DoIt
NSLog( @"Dimension in integer \"x\", but do not assign it a value.\n" )
long x
// Note that the @ (at sign) prefixing x is a pointer to its machine address
NSLog( @"The machine address of x is: %p", @x )
NSLog( @"While x is unassigned, the machine address will contain a garbage value: %ld\n", x )
// Assign x a value of 1234
x = 1234
NSLog( @"When x is assigned a value of %ld, that value will be stored in the machine address: %p", x, @x )
NSLog( @"The machine address now contains the value: %ld\n", x )
// Reassign x a value of 5678
x = 5678
NSLog( @"Wnen x is reassigned the new value %ld, that value will be stored in the existing machine address: %p", x, @x )
NSLog( @"The machine address now contains the value: %ld\n", x )
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Dimension in integer "x", but do not assign it a value.
 
The machine address of x is: 0x7ffee279bb58
While x is unassigned, the machine address will contain a garbage value: 1099524915200
 
When x is assigned a value of 1234, that value will be stored in the machine address: 0x7ffee279bb58
The machine address now contains the value: 1234
 
Wnen x is reassigned the new value 5678, that value will be stored in the existing machine address: 0x7ffee279bb58
The machine address now contains the value: 5678
 
</pre>
 
 
=={{header|Go}}==
Line 1,071 ⟶ 1,110:
Note that it is not possible to specify the address at which the embedding API function ''wrenSetSlotNewForeign'' allocates new objects and any attempt to allocate a new object at the same address as an old one by juggling with pointers will almost certainly lead to a seg fault. So all we can sensibly do is to change the value of the current object.
 
<syntaxhighlight lang="ecmascriptwren">/* create_object_at_given_addressCreate_an_object_at_a_given_address.wren */
 
import "./fmt" for Fmt
Line 1,093 ⟶ 1,132:
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc create_object_at_given_addressCreate_an_object_at_a_given_address.c -o create_object_at_given_addressCreate_an_object_at_a_given_address -lwren -lm */
 
#include <stdio.h>
Line 1,208 ⟶ 1,247:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "create_object_at_given_addressCreate_an_object_at_a_given_address.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,233 ⟶ 1,272:
Integer object value changed to: 43 but still at address 0x55a56e0f2dd8.
</pre>
 
=={{header|Z80 Assembly}}==
When writing assembly yourself, you'll know any object's memory location in advance.
9,476

edits