# Ptr math, Integer or UInteger

**URL:** <https://forum.xojo.com/t/ptr-math-integer-or-uinteger/33300>\
**Category:** General\
**Created:** [September 8, 2016, 5:42am UTC](https://forum.xojo.com/t/ptr-math-integer-or-uinteger/33300 "2016-09-08T05:42:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [September 8, 2016, 5:42am UTC](https://forum.xojo.com/t/ptr-math-integer-or-uinteger/33300/1 "2016-09-08T05:42:13Z")

</div>

I’m moving a Ptr around a MemoryBlock like this…

```auto
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 )
next
```

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.

---

<div class="post-metadata">

**Author:** ![jim\_mckay](https://forum.xojo.com/user_avatar/forum.xojo.com/jim_mckay/32/905_2.png) [@jim\_mckay](https://forum.xojo.com/u/jim_mckay)\
**Post date:** [September 8, 2016, 3:01pm UTC](https://forum.xojo.com/t/ptr-math-integer-or-uinteger/33300/2 "2016-09-08T15:01:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [September 18, 2016, 6:05am UTC](https://forum.xojo.com/t/ptr-math-integer-or-uinteger/33300/3 "2016-09-18T06:05:09Z")

</div>

Sorry, I got sidetracked.

> [@286029:@jim mckay](#):
>
> Xojo wraps signed integers for us without complaining

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).

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:27pm UTC](https://forum.xojo.com/t/ptr-math-integer-or-uinteger/33300/4 "2020-10-29T18:27:10Z")

</div>


