pointer casting again ;)

BlitzMax Forums/BlitzMax Programming/pointer casting again ;)

col(Posted 2011) [#1]
Hi all
I have a pointer setup as

local memptr:byte ptr

this points to some address with data of say 82 bytes in length

I also have a type object, lets call it OBJ. The fields inside OBJ take up 82 bytes of memory.

Is there anyway i can typecast so that the address of the first field in a new OBJ will be the same address of memptr ??
I know i could use the MemCopy command, but in the real program this isnt a real option, this is just an example and everything would be cured with a simple typecast.

thanks for all your help

Last edited 2011

oh!.... the fields inside obj are floats and integers, floats first then several integers

Last edited 2011


Czar Flavius(Posted 2011) [#2]
Why can't you use memcopy? Make a second pointer to the address of this field and copy away!

Or make a float pointer and an int pointer. Read a few floats based on the pointer using array syntax eg [4] to get the 5th float from the beginning, and either store in the right field in the type (eg float_field_5) or write to [4] of a second float pointer you have pointed to the beginning of the type. And then do the same with the int pointer starting the index at the right place.

Copying to specific fields manually will be more tedious but safer. The type's data structure could consist of hidden padding or other meta data such as type information/virtual function pointers that are handled under the hood. If it's a simple type with no extensions or methods this is less likely to be the case.


col(Posted 2011) [#3]
Thanks Czar Flavius

I suppose I'll just use the pointer to pointer transfer method. I'll just make it into a function and use that.
Its no big deal but I thought there was a way to simply type-cast and I'd forgotten it.

Thanks again

Edit.... I know I said that it's not a real option - of course it is an option - I just didnt want to do it :-) lol
Its done now and working perfect.

Last edited 2011


AdamRedwoods(Posted 2011) [#4]
Is this what you're looking for?
http://www.blitzbasic.com/Community/posts.php?topic=55939

Also read up on type safety , and why object<->byte ptr isn't allowed automagically (although it says it can be done in the Blitzmax docs):
http://www.blitzbasic.com/Community/posts.php?topic=76234

And if you're trying to do this to/from C or C++ there are easy ways:
http://www.blitzbasic.com/Community/posts.php?topic=90971

SuperStrict

Type TOther
	Field f:Float = 0.05
	Field i:Int =5
	Field d:Double = 2.34
	Field s:String = "xyz"
EndType

Local data:TOther = New TOther



Type TType
	Field f:Float
	Field i:Int
	Field d:Double
	Field s:String
EndType

Local j:Byte Ptr = Byte Ptr (data)

Local k:TType
(Byte Ptr Ptr Varptr k)[0] = j-8

Print (k.s)

End


Last edited 2011

Last edited 2011

Last edited 2011


Gabriel(Posted 2011) [#5]
Also read up on type safety , and why object<->byte ptr isn't allowed automagically (although it says it can be done in the Blitzmax docs):

It used to be possible, which might be why the docs say it is. In fact, it used to be worse than possible, it was possible implicitly. That's to say, you could assign a byte ptr to an object without even typecasting. I complained about that a lot, but I'm not surprised that it didn't get fixed until it bothered someone else too ;)

I actually wouldn't have minded it being possible with typecasting. I wouldn't have used it personally, but I can see how it might have been useful for a quick and dirty solution. I'm glad the implicit casting possibility was removed though. That wasn't good in a supposedly typesafe language.


col(Posted 2011) [#6]
Hi all,
To be more specific with what I was doing.....

In C++ :

MATERIAL* pMaterial = (MATERIAL*)pMtrlBuffer->GetBufferPointer();


I've done it now and it works perfect.

Last edited 2011