INT to object?

Started by Sinjin, October 13, 2023, 19:11:26

Previous topic - Next topic

Sinjin

How do I put integer into an object? I know any type works fine but if I need a simple INT its not possible.
local o:object=string"ff" 'ok
local o:object=16 'not ok
Or in other words: How do I allocate memory for 1 integer? lol I dont need a type. Isnt there a way to read the address directly? INT PTR and VARPTR comes to mind but for some reason it wont work.
I can do it in ASM:
;in esp+4 object
;out eax=adr
public _getaddress
_getaddress:
  mov eax,[esp+4]
ret
SetAddress wouldn't be hard to code, sure thats not a real pointer, but is there a way in Bmax itself?

dawlane

#1
Quote from: Sinjin on October 13, 2023, 19:11:26How do I put integer into an object?
You cannot. Unless you box it. e.i. Wrap the integer inside a type. Then you have to use methods to get at the data.
Though you could try using a single element array.

Quote from: Sinjin on October 13, 2023, 19:11:26How do I allocate memory for 1 integer? lol I dont need a type.
All primitive data types have automatic memory allocation. You do that every time you use Local, Global etc.

Quote from: Sinjin on October 13, 2023, 19:11:26Isnt there a way to read the address directly? INT PTR and VARPTR comes to mind but for some reason it wont work.
VarPtr will get the address of a variable, but you have to make sure that the variable you will be storing the address in is of adequate size e.i. ULong on a 64 bit builds.
You can then dereference the address to get it's value directly with Int Prt.
Local an_int:Int=16
Local an_int_address:ULong = Varptr an_int ' An address will need storage of 8 bytes on 64 bit build.
Print Int Ptr (an_int_address)[0] ' Dereference address to get the value.