Author Topic: [Bmax] Passing entire objects by pointer?  (Read 5095 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile


I've taught myself how pointers work in C++ and really liked them. Now I want to learn them in bmax. I've gotten the basics however run into some problem when trying to pass and object by pointer. According to the documentation you may pass objects by pointer and the index of the pointer passes the field variable mapped to that index. With that said, I don't understand why this code is not working...

Code: [Select]

' demonstrates pointers
SuperStrict

'///////////////////////////////////////////
' first we test passing an int by pointer...
'///////////////////////////////////////////


'create a test int and assign it value
Global the_number:Int = 1998

'create out int pointer, use varptr to assign it to the_number
'a pointer must point to a memory location, varptr does this
Global pointer:Int Ptr = Varptr(the_number)

'finally print the value pointed to by pointer
rem
since a pointer points to a point in memory
we want where it is pointing. To do this, we just
want the index it is pointing to. This means that we
want memory index 0, the starting point of the pointed
to memory.
endrem

'recall the value of the pointed to memory
Print(pointer[0])


'///////////////////////////////////////////
'test passing an array by pointer...
'///////////////////////////////////////////

Global array:Float[] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]

' arrays are not directly supported, however they can still be passed
Global array_pointer:Float Ptr = Varptr(array[0])

rem
arrays are stored in a linear fashion, so if we pass the memory
location of the first index, we may iterate throught the entire
array!
endrem

For Local i:Int = 0 To array.dimensions()[0] - 1
Print(array_pointer[i])
Next

'///////////////////////////////////////////
'test passing a complex object by pointer...
'//////////////////////////////////////////


rem
since we do not have direct object pointers in Blitzmax,
we are forced to use byte pointers! In this case,
the first index of the pointer is the first field value
of the object in question. Lets try it
endrem

rem from the docs
BlitzMax will automatically convert any pointer type to a byte pointer
for you. In addition, objects and arrays may be assigned to byte pointers.
In the case of objects, the pointer will contain the address of the
first field of the object. In the case of arrays, the pointer will
contain the address of the first element of the array. Be very careful
when assigning objects to pointers, as there is a danger that the garbage
collector will 'reclaim' the object before you have finished with the
pointer! It is recommended that you use GCSuspend and GCResume around code
that converts objects to pointers.
endrem

Type human
Field age:Byte = 22, name:String = "Mr. Burnside"
End Type

GCSuspend()
Global ryan:human = New human
Global human_pointer:Byte Ptr = Varptr(ryan)

'print his attributes
Print(human_pointer[0])
Print(human_pointer[1])

GCResume()
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #1 on: June 23, 2009 »
Can you describe 'not working' a bit better?

It looks OK, but I suspect the last section (human, ryan) isn't printing what you expect.

Jim
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #2 on: June 23, 2009 »
Unless you can find some information about exactly how the type structure is setup, you will have to do something like:

Code: [Select]
Global human_pointer:Byte Ptr = Varptr(ryan.age)
That at least works for your Age byte field, but strings are stored differently, if I remember correctly, its not just one byte per character, I think its two bytes for each, but you might have to do a seach on how to deal with that on the blitzmax forum, I am sure someone has asked about that at some point.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #3 on: June 23, 2009 »
Ahhh Ok

'BlitzMax will automatically convert any pointer type to a byte pointer for you. In addition, objects and arrays may be assigned to byte pointers. In the case of objects, the pointer will contain the address of the first field of the object"

I thought changing the index of each pointer would cycle through the field variables. I guess this was kind of a stupid notion.
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #4 on: June 23, 2009 »
Quote
I thought changing the index of each pointer would cycle through the field variables.

I am not sure there is a way to do that, but its not something I have had a need for, so I have not looked for a way to do it either.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #5 on: June 23, 2009 »
I'd guess that int and float would be +4, pointing at 4 bytes of data, string would be +4 too, value is 4 byes of another pointer to the string in wide char 2 bytes each.  But there might be gaps or padding between the fields, or they may not be in the order you declared them in - if it were C I could tell for sure but not in PB.

Jim
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #6 on: June 23, 2009 »

I guess Blitz really is not meant for working much with pointers. C++ makes it much more simple. There isn't a whole lot one can do with just being able to pass the primitive types such as int and float. I'd imagine reconstructing a complex object bit by bit from passed pointers may invalidate the speed gained from using them. It sure would be great to be able to pass very large complex objects without having to make a call to a reference.
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [Bmax] Passing entire objects by pointer?
« Reply #7 on: June 24, 2009 »
For bmax only code why not just use the variables directly and access the type fields using they type itself? If you are mixing C and bmax then I can see how it might be an issue though, but I know that some users on the blitzmax forum does mix it up, so it should be possible somehow, but might need some kind of code to act between the bmax and c code.