Add an offset to a Byte Ptr

BlitzMax Forums/BlitzMax Programming/Add an offset to a Byte Ptr

JoshK(Posted 2007) [#1]
If a function accepts a Byte Ptr as a parameter, is this a correct way to indicate an offset?:

somefunction bank.buf()+4


Brucey(Posted 2007) [#2]
Yep. That should work.


Grey Alien(Posted 2007) [#3]
Buf() returns a Byte Pointer so yes that's correct. If you had a type and wanted the memory address of that, you'd use varptr(mytype)


ImaginaryHuman(Posted 2007) [#4]
Please note that if you add 4 to a byte pointer you are adding 4 BYTES to the address, whereas for example adding 4 to an Int pointer actually adds 16 bytes, and short pointers would add 8 bytes.


Grey Alien(Posted 2007) [#5]
Cripes I never knew that, I would have assumed it ALWAYS added bytes only. Fair enough, it's because I used to use Bytes when it was "normal" in computers of the olden day.


ImaginaryHuman(Posted 2007) [#6]
Yah any value based math that you do with a pointer of a particular type, the values are converted to the `sizeof` the type, so:

BytePtr+1 = Byte Pointer Address + 1
ShortPtr+1 = Short Pointer Address + 2
IntPtr+1 = Int Pointer Address + 4
IntPtr + Offset = Int Pointer + (Offset * 4)

This is why when you design your algorithm which uses pointers, e.g. to move data around or work with pixels in a pixmap, you have to remember that the amount that you increment the pointer by depends on its type, so you can skip through RGBA pixels by adding 1 to an Int Pointer, but if you're working off a Byte Pointer you have to add 4.