Simple type question..?

BlitzMax Forums/BlitzMax Beginners Area/Simple type question..?

Ole JR(Posted 2004) [#1]
Example:
Type mytype
  Field a, b
EndType
Function test(x:mytype)
 'Here do something to x:mytype
EndFunction

Function Blah()
 typ1:mytype = New mytype
 typ2:mytype = New mytype

 typ1.a = 10
 typ1.b = 5

 'Make typ2 like a "temp" of typ1
 'Change typ2 without(!) changing anything in typ1.. 
 typ1 = typ2 'Does not work

 test(typ2)
EndFunction

In C I would do something like:
struct mytype
{
char a;
char b;
};
void test(struct mytype *ptr)
{
// Change something
}
void Blah(void)
{
 struct mytype typ1;
 struct mytype typ2;
 typ1.a = 10;
 typ1.b = 5;
 
 typ1 = typ2;
 test(&typ2);
}

The typ1=typ2 thingy is my problem. It makes them identical
but when I change something in either typ1 or typ2 the other also change.. Not what I want.. Anyone??


tonyg(Posted 2004) [#2]
Typ1 and typ2 are variables which hold a pointer to your type object in memory.
By stating typ1=typ2 you will point both variables to the same object and lose the pointer to the first object you created.


Ole JR(Posted 2004) [#3]
I know, when doing typ1=typ2 they are pointing to the same object.. But how do I create a 'temp' of typ1 that I can change without doing anything with it??


tonyg(Posted 2004) [#4]
In Blitzbasic you'd have to create a new type and copy the field data. I haven't seen anything in Bmx to suggest otherwise.
Might be a clever way of finding the pointer to the type in memory but, basically, that's what you'd be doing with the new type.


Ole JR(Posted 2004) [#5]
Yes did that in Blitzbasic/3D..
But I think there's another way now.. Hope so anyway..
Use VarPtr or something...


tonyg(Posted 2004) [#6]
Whatever you do with the variable containing the pointers the area of memory will remain the same.
The only way to change the actual values without changing those of the initial value is by duplicating the memory and creating a new pointer either manually or with a new type.
I suppose you could create a function which took a backup and another that restored.
P.S. I could be very wrong here.


Ole JR(Posted 2004) [#7]
Don't know if I'm way off here..
But if I do (in the example earlier):
Print Int(VarPtr typ1)
Print Int(VarPtr typ2)

They return different "something" (memory-address??)
*IF* that's the right way to use VarPtr..


tonyg(Posted 2004) [#8]
Hmm. very odd.
I can only assume that ship now points to the address of ship2 which points to the instance values.
How could you find out what is actually at the pointer address? CreateRamStream?


Who was John Galt?(Posted 2004) [#9]
Are you picking up the address of the pointers rather than the address they point to? Not sure - haven't used this stuff yet.


Bot Builder(Posted 2004) [#10]
Type Cow
	Field Name$
	Field Size
	
	Method Moo()
		Print "                                    "+Name$+" Moos"
	EndMethod

EndType

C1:Cow=New Cow
Print "Original object:"
C1.Name="Fat arse"
C1.Size=200
C1.Moo()

C2:Cow=New Cow
CopyType(C1,C2)
Print "New, copied object:"
C2.Moo
Print "New object's name changed to fred"
C2.Name="Fred"
Print "New object:"
C2.Moo
Print "Original object:"
C1.Moo

Function CopyType(Source:Cow,Dest:Cow)
	Local ST:Cow Ptr=Varptr Source, DT:Cow Ptr=Varptr Dest
	Local S:Byte Ptr=Byte Ptr(ST[0]), D:byte Ptr=Byte Ptr(DT[0])
	Local SLeng=SizeOf(Source), DLeng=SizeOf(Dest)
	If SLeng<>DLeng Then Throw "The objects must be the same to copy them."
	For Local i=0 Until SLeng
		D[i]=S[i]
	Next
EndFunction
This works, but I'd rather the copy function was generalized. I can seem to find the Sizeof() an object. yet. SizeOf(o:Object) always returns 0.