PDA

View Full Version : when it's time to use a pointer?



largo_winch
30-11-2011, 18:39
simple and general question, but I want to know where's the pro and cons for using pointers. I send a working example. Perhaps anybody can explain his experience or his opinion with critics with using or not using pointers. I've read some articles from c++ websites but they are confusing me ;)


' Empty GUI script created on 11-30-2011 17:18:01 by largo_winch (ThinAIR)

Uses "Console"

Type myUdt
lName As String
age As Long
End Type

Dim myUdtVariable As myUdt
Dim myUdtArray(4) As myUdt

' -- Initialize data
myUdtVariable.lName = "Largo"
myUdtVariable.age = 128

' -- Initialize data
myUdtArray(1).lName = "MarieClaire"
myUdtArray(1).age = 52

myUdtArray(2).lName = "Sylvia"
myUdtArray(2).age = 164

myUdtArray(3).lName = "Roberto"
myUdtArray(3).age = 96

myUdtArray(4).lName = "Petra"
myUdtArray(4).age = 41

' -- Simple call
PrintL "Simple myUdtVariable:"
ListNames(myUdtVariable, 1)
PrintL

' -- Array call
PrintL
PrintL "Simple myUdtArray:"
PrintL
ListNames(myUdtArray(1), UBound(myUdtArray)) ' -- You pass first member (not just name!), then number of elements


WaitKey

Function ListNames(ByRef myUdtVariable As myUdt, ByVal numberOfItems As Long) As Long
' -- Get pointer to the passed data (variable, or first member of the array)
Local PointerToData As DWord
PointerToData = VarPtr(myUdtVariable)

' -- Memory overlay over passed data - for single value it will be just one dimensional
' -- for array it will make all elements accessible (as long as numberOfItems is correct)
Local Values(numberOfItems) As myUdt At PointerToData

Local i As Long

' -- Loop through names and print them
For i = 1 To numberOfItems
PrintL Values(i).lName
Next

Return %TRUE
End Function

addendum:
a) pointers: "...they provide significantly greater speed in performing memory operations" that's true I am thinking. But it's always useful to use pointers?
b) "A pointer is simply a variable that holds a 32-bit (4 byte) address, typically an address where the data associated with a variable is found. For example, if an integer variable i% = 4, the value 4 is location in memory somewhere. A pointer to i% would be the address in memory where the value 4 is located."



bye, largo

dcromley
30-11-2011, 19:20
Largo,

I've used pointers in the past, but I can't come up with an example.
This is a poor example for using pointers, because TB allows passing
a UDT parameter array. See code below.

Are you looking for "significantly greater speed"? I prefer simplicity.

Regards, Dave

Uses "Console"

Type myUdt
lName As String
age As Long
End Type

Dim myUdtArray(4) As myUdt

' -- Initialize data
myUdtArray(1).lName = "MarieClaire"
myUdtArray(1).age = 52
myUdtArray(2).lName = "Sylvia"
myUdtArray(2).age = 164
myUdtArray(3).lName = "Roberto"
myUdtArray(3).age = 96
myUdtArray(4).lName = "Petra"
myUdtArray(4).age = 41

PrintL "Printing the array:" & $CRLF
ListNames(myUdtArray)
WaitKey

Sub ListNames(myUdtVariable() As myUdt)
Local i As Long
' -- Loop through names and print them
For i = 1 To UBound(myUdtVariable)
PrintL myUdtVariable(i).lName
Next
End Sub

largo_winch
01-12-2011, 15:16
thanks dave, but I wanted to know more about pointers where I can have more advantage of using them in functions and udt styles. I have found another example that works now and checks a string pointer. where do you're using pointers at work? in another board a user complains he's never working with pointers so I was very astonished about that argument ;)


' Empty GUI script created on 12-01-2011 13:42:26 by (ThinAIR)

Uses "console"

Type ANIMAL_LIST_TYPE
pNext As DWord Ptr 'ANIMAL_LIST_TYPE Ptr '' you cannot use this in current Thinbasic issue ! :)
pPrev As DWord Ptr 'ANIMAL_LIST_TYPE Ptr '' you cannot use this in current Thinbasic issue ! :)
zKey As Asciiz Ptr
zData As Long
nType As Integer
End Type

Dim myANIMAL_LIST_TYPE As ANIMAL_LIST_TYPE

Type ANIMAL_LIST_HEADER_TYPE
pFirst As ANIMAL_LIST_TYPE Ptr
pLast As ANIMAL_LIST_TYPE Ptr
UseStrings As Byte
SortList As Byte
COUNTS As Long
End Type

Function TBMain() As Long
MsgBox 0, fictive_getStringDatabyPosition(1)

End Function

Function Fictive_GetStringDataByPosition(ByVal nPosition As Long) As String

Local pList As ANIMAL_LIST_HEADER_TYPE 'Ptr
Local pListItem As ANIMAL_LIST_TYPE 'Ptr
Local x As Long
Local pListAddress As DWord

pList = pListAddress

If pList = 0 Then
Exit Function
End If

' Iterate to the specified ordinal position
'pListItem = @pList.pFirst
pListItem = VarPtr(pList.pFirst)
x = 1
Do Until x >= nPosition
'pListItem = @pListItem.pNext
pListItem = VarPtr(pListItem.pNext)
If pListItem = 0 Then Exit Do
Incr x
Loop
'If @pListItem.zData Then
If VarPtr(pListItem.zData) Then
Function = RetrieveString(VarPtr(pListItem.zData) )
End If

End Function

Function RetrieveString(ByVal zData As Long) As String
Local pS As String 'Ptr

If zData = 0 Then
Function = ""
Else
pS = zData
Function = StrPtr(pS)
End If

End Function



the example was just a test for me to understand how string pointers (strptr) and varptr are working with udt styles with thinbasic. thanks anyway.

bye, largo

dcromley
01-12-2011, 20:16
> .. but I wanted to know more about pointers ..
That's admirable, and you seem to be doing well.
I'm going to bow out of this thread.
However, I do question your last statement in:


Type ANIMAL_LIST_HEADER_TYPE
pFirst As ANIMAL_LIST_TYPE Ptr
pLast As ANIMAL_LIST_TYPE Ptr
UseStrings As Byte
SortList As Byte
COUNTS As Long
End Type

Local pList As ANIMAL_LIST_HEADER_TYPE 'Ptr

If pList = 0 Then ' what are you intending here?
Regards, Dave