Get Array Pointer

Started by kevin, July 21, 2005, 03:35:04 PM

Previous topic - Next topic

kevin

  This example shows a new feature of PlayBasic V1.082.  Which allows your to grab the address( pointer) of an array in memory. You can then peek/poke this memory as you like.

 This will be mostly useful for DLL's/Plug In authors wishing to pass Arrays to their Dll's directly.  Although users could use it as way to store Mixed data within a single array.  Since you can poke/peek Bytes/Words/Integers/Floats and Strings. Obviously some care has to be taken when doing that. But, you can now do it.



   Version for PlayBASIC V1.08 (Pre 2005)  


PlayBASIC Code: [Select]
   ; Create Array Called Table()
Dim Table(10)
For lp=0 to 10
Table(lp)=100+lp
Next

; Get the Addres of this array
Address=GetArrayPtr(table())

; peek the array memory and show the elements
ShowArray(Address)

; manually poke some new values into this arrays memory
print "Poking Data Directly Into This Arrays Memory"
DataAddress=Address+PBarraystruct_size
For lp=0 to 10
PokeInt Dataaddress+(lp*4),2000+lp
next

;show the array memory again
ShowArray(Address)

sync
waitkey

Function ShowArray(Address)
print "Display Array Data"
DataAddress=Address+PBarraystruct_size
For lp=0 to 10
print PeekINt(Dataaddress+(lp*4))
next
EndFunction





   PlayBASIC V1.64 (and above Version)  

PlayBASIC Code: [Select]
   ; ---------------------------------------------------------------   
; PlayBASIC V1.64 (and above Version)
; ---------------------------------------------------------------
;
; GetArrayPTR() includes an optional flag to either return
; the arrays RAW address in memory or the address
; of the FRIST data stored at INDEX #0
;
; ---------------------------------------------------------------



; Create Array Called Table()
Dim Table(10)
For lp=0 to 10
Table(lp)=100+lp
Next

; Get the Address of the INDEX 0 within this array
Address=GetArrayPtr(table(),true)

; peek the array memory and show the elements
ShowArray(Address)

; manually poke some new values into this arrays memory
print "Poking Data Directly Into This Arrays Memory"
For lp=0 to 10
PokeINT address+(lp*4),2000+lp
next

;show the array memory again
ShowArray(Address)

sync
waitkey

Function ShowArray(Address)
print "Display Array Data"
For lp=0 to 10
print PeekINT(address+(lp*4))
next
EndFunction






 Learn to code PlayBASIC  @ https://playbasic.com/help.php