Jump to content

Create an object at a given address: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
Line 293:
</lang>
Some architectures may require special fetch and store operators to access ports. For example, [[Open Firmware]] defines l@ and l! for safe 32-bit port writes.
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0
 
Type Person
As String name
As Integer age
Declare Constructor(name As String, age As Integer)
End Type
 
Constructor Person(name As String, age As Integer)
This.name = name
This.age = age
End Constructor
 
Dim ap As Any Ptr = CAllocate(SizeOf(Person)) ' allocate memory to store a Person object
 
'create a Person object at the address of the memory we've just allocated
 
Dim p As Person Ptr = New(ap) Person("Teresa", 60)
 
'check addresses are same
Print ap, p
 
'check data is not corrupt
Print p -> name, p -> age
 
'call implicit destructor
p -> Destructor
 
'free memory
Deallocate(ap)
 
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
4790800 4790800
Teresa 60
</pre>
 
=={{header|Go}}==
Go has several ways to access arbitrary memory locations using the built-in unsafe package. If the desired memory contains an array, since Go doesn't have pointer arithmetic, then a slice should be used instead of a pointer. The following solution demonstrates both a pointer and a slice.
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.