I’m moving a Ptr around a MemoryBlock like this…
dim mem As new MemoryBlock(100)
dim baseAddr As Integer = Integer( CType(mem, Ptr) )
dim p As Ptr
for i As integer = 0 to 9
p = Ptr( baseAddr + i * 10 )
callMyDeclare( p )
Should baseAddr be Integer or UInteger?
I’ve used Integer in the past without problems but now I’m thinking I’ve just been lucky and should be using UInteger.
Same thought process here. It seems we’ve been lucky in the sense that Xojo wraps signed integers for us without complaining, but I would err on the side of safety and use UIntegers instead.
example of “wrapping”
dim p,p2 as ptr
dim i as integer=&hffffff00 // i is -256
dim ui as uinteger=&hffffff00 //ui is 4294967040
p=ptr(i) //p is &hffffff00
p2=ptr(ui) //p2 is &hffffff00
From the LR
“If you assign a value that is larger than what the integer type can hold, then the value will “overflow”. This usually means the value will wrap around. This applies to all the data types.”
The word “usually” makes me nervous here as bad pointer math can quickly lead to a hard crash.
Sorry, I got sidetracked.
So the only real difference between Integer and UInteger is the interpretation, ie the resulting bit pattern will be the same after addition and multiplication. I’ll use UInteger then just be be clear/consistent.
And everything in the expression has to be UInteger. (UInt + Int1 * Int2) produces an Integer which gets cast to a UInteger of the intended value, but just to have no worries i’ll use (UInt1 + UInt2 * UInt3).