pointers

BlitzMax Forums/BlitzMax Beginners Area/pointers

yossi(Posted 2014) [#1]
i understand that pointer is a special variable that hold address of another variable but i cant understand the usage of it.

i wonder if anyone here has ever used pointers with blitz max
and if so on what purpose and circumstances ?

i want to understand the benefits of working with pointers if any.


Henri(Posted 2014) [#2]
Pointers are fundamental in computer language when you think in terms of memory. It is more efficient to move address of a data around (in Blitzmax memory address is 4 bytes) then the whole data (which can be of any size). Raw pointers are not usually associated with higher level languages like basic, but inside, at lower level they are used all the time.

Normally raw pointers are not needed in Blitzmax, but when you start messing with C, and adding C-components to your application you quickly see the need for one.

So, to summarize: It's a handy way to move large amount of data, without moving large amount of data :-)

-Henri


Brucey(Posted 2014) [#3]
i wonder if anyone here has ever used pointers with blitz max

Once or twice.

Most people using BlitzMax will never need to use them, or even know about them.
They are especially useful for interfacing BlitzMax with externally created data - like in a 3rd party library. You might hold the reference to some data that you can then use later on when you communicate again with the library.


yossi(Posted 2014) [#4]
thank you.


Mr. Goober(Posted 2014) [#5]
A byte pointer is also required to access the pixels of a TPixmap object, which one would obtain if an image is locked using LockImage().

Pointers in BlitzMax are accessed in an array-style fashion, making it easy to understand how the data is accessed if you know the pointer data type. Pointers in BlitzMax must be one of the basic Data Types such as Byte, Short, Int, and Long. This tells BlitzMax how large the bytes of data between intervals is supposed to be:

Byte = 1 Byte (8 bits)
Short = 2 Bytes (16 bits)
Int = 4 Bytes (32 bits)
Long = 8 Bytes (64 bits)

superstrict
local a :int = 9000
local b :byte ptr = VarPtr(a)
    b[0] = 255 (put 1s for bits 0-7)
    b[1] = 255 (put 1s for bits 8-15)
    b[2] = 255 (put 1s for bits 15-23)
    b[3] = 255 (put 1s for bits 24-31)
print a ' will display -1 because Int is a signed data type



Who was John Galt?(Posted 2014) [#6]
You can think of a blitz types in terms of pointers. E.g:
Type Egg
   field yolk
End Type

egg1:Egg=new Egg
egg2:Egg=egg1
egg1.yolk=5
'if we print egg2.yolk, it will be 5

egg1 and egg2 contain behind the scenes a pointer to the actual memory where those instances are stored. When we set egg2=egg1, we are not copying the contents of the two types, we are literally pointing the variables egg1 and egg2 at the same block of memory. In other words, we have only one egg ('new' was called only once) with two references (pointers) to it.


ImaginaryHuman(Posted 2014) [#7]
I use them quite a lot when I want to process images or other pixel-like data. ie instead of using stuff like WritePixel or ReadPixel, or Poke or Peek, I prefer to get the pointer to the base of the memory e.g. PixmapPixelPtr(MyPixmap,0,0) or BankBuf(MyBank), and then increment the pointer (or an offset to it) and use that to access the raw data. Pointers can be faster if you use them for this kind of thing.