Pointers to type instances and fields

Blitz3D Forums/Blitz3D Programming/Pointers to type instances and fields

eNano(Posted 2015) [#1]
Hi, some time ago I was playing around with pointers and I was studying about types and fields.
Just to share a little I put here what I've got so far
(Sorry if maybe I don't use the correct names for each concept, I'm not a coder, just curious)

Suppose you have a type like this:

Type test
field IntegerField%
field FloatField#
field ArrayField%[3]
field StringField$
End Type

When you create an instance you do:
test1.test = new test

If you want to see an integer value that represents that instance you can write:
test1Pointer = Int(test1)

this give us a memory address that represents this instance, and
If you want to access each field of that instance you just have to start reading 20 bytes after this integer, and then each value is separated by 4 bytes intervals

for example the value of the first field(integer) is this:
MemoryPeekINT ( testPointer+20 )
this will return the value of the first field

the second field is:
MemoryPeekFLOAT ( testPointer+20+4 )
this will return the value of the second field (a FLOAT)

To access a field of the array kind you have to get the pointer of that array first, and then get each element in a 4 bytes interval.

For example the pointer for the array field is
arrayFieldPointer = MemoryPeekINT ( testPointer+20+4+4 )
and from that point you can get each value in the correct format (in this case we have an integers array)
If I want to get the third value I write:
MemoryPeekINT (arrayFieldPointer + 8 (0 based count))

Having shared this I have a question: What about strings?
I can't figure out how to access them, this is what I have so far:
pointerToStringField = MemoryPeekINT ( testPointer+20+4+4+4 )
stringLEN = pointerToStringField + 8

WHERE SHOULD I READ THE EACH CHAR??
THANKS!


RemiD(Posted 2015) [#2]
Not sure if you are doing this for fun or whatever but :

are you aware of the Handle(instance) and Object.Type(instancereference) functions ?
With Handle(instance) you can get the unique reference ("handle") of an instance of a Type.
With Object.Type(instancereference) you can directly access this instance of a Type.

Maybe this can help you...


Floyd(Posted 2015) [#3]
No idea, but Blitz3D is open source now. So it is certainly possible to find out how strings (and everything else) are implemented.


Yasha(Posted 2015) [#4]
Maybe this can be of help: String banks/data buffers

Code to modify the characters of strings in-place (which breaks all of the rules, but ehh).


eNano(Posted 2015) [#5]
Yes thanks RemiD I was aware of that, but I found interesting the flexibility of the pointers. With object and handle you just can work with a specific type and you cant have direct access to a field value with the returned value (in an abstract way, I mind you have to write in the code which field you want to know). With integer references you can read and write to any type or any field of a type avoiding duplicated functions and saving in some cases a lot of loops to update changes.

Thanks Floyd but I find difficult to read big codes because I've very basic knowledge on coding

Thanks a lot Yasha! I've been following this community for a couple of years and I've learned a lot from your posts thanks for sharing!
The only info I had about this topic is from that post you say, but I can't fully understand it I'll keep trying!