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
' 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