Byte Ptr Ptr

BlitzMax Forums/BlitzMax Programming/Byte Ptr Ptr

Henri(Posted 2014) [#1]
Out of curiosity. What is the need for Byte Ptr Ptr ? How I understand it its a pointer to a pointer ?

-Henri


Brucey(Posted 2014) [#2]
Yes, it's a pointer to a pointer, or a variable who's contents point to another variable who's content is a pointer. Heh.

I use type Ptr Ptr far too much, I think.

BlitzMax arrays use it internally, for accessing the array contents.

It can be useful in different ways.

For iterating over a block of pointers :
SuperStrict

Framework brl.standardio


Local p:Byte Ptr = MemAlloc(4 * 20)

Local iter:Int Ptr Ptr = Int Ptr Ptr (p)

For Local i:Int = 0 Until 20
	iter[i] = 0 ' some pointer to somewhere
Next



Here's another not-so-useful piece of code which demonstrates... hmm... something.
Imagine, if you will, that we need to use some external process to allocate some memory that we want to use. We create a byte ptr variable, and send the address of that variable to the external allocation function.
When the function returns, we have a block of memory pointed to by our variable.
SuperStrict

Framework brl.standardio


Local p:Byte Ptr

externalMemAlloc(Varptr p)

For Local i:Int = 0 Until 128
	Print p[i]
Next

externalMemFree(Varptr p)

Function externalMemAlloc(p:Byte Ptr Ptr)
	p[0] = MemAlloc(128)
	populate(p[0])
End Function

Function externalMemFree(p:Byte Ptr Ptr)
	MemFree(p[0])
End Function

Function populate(p:Byte Ptr)
	For Local i:Int = 0 Until 128
		p[i] = i
	Next
End Function



And so it turns out I'm not very good at explaining it very well :-p


Henri(Posted 2014) [#3]
I have to dwell on that a bit :D

-Henri


zoqfotpik(Posted 2014) [#4]
Brucey you seem to do quite a lot of stuff which is basically pure research. What do you get out of it? Do you do things just because they are interesting? Perish the thought!


Azathoth(Posted 2014) [#5]
Pointer to pointers are common in C/C++ programming.


Henri(Posted 2014) [#6]
Hmm...you could also write the example without pointing the pointers;)
SuperStrict

Framework brl.standardio

Local p:Byte Ptr
p = externalMemAlloc(p)

For Local i:Int = 0 Until 128
	Print p[i]
Next

externalMemFree(p)

Function externalMemAlloc:Byte Ptr(p:Byte Ptr)
	p = MemAlloc(128)
	populate(p)
	Return p
End Function

Function externalMemFree(p:Byte Ptr)
	MemFree(p)
End Function

Function populate(p:Byte Ptr)
	For Local i:Int = 0 Until 128
		p[i] = i
	Next
End Function


-Henri


Brucey(Posted 2014) [#7]
No, that's something else entirely.

If you imagine that you are using some library that requires you to pass in the address of the pointer, then you'd have to use a Ptr Ptr.

DirectX seems to do this a lot, for example (I've had enough of DirectX for today...)


Henri(Posted 2014) [#8]
I haven't immersed myself into native DirectX yet so I take your word for it.

-Henri