Page 1 of 1

Problem with pointers to structures.

Posted: Tue Apr 02, 2024 12:27 am
by Armoured
Could someone help me figure out what's wrong with this code? I can't seem to understand it.
In theory, I should be able to change the values of "Field1" or "Field2" by associating the pointers contained in the array memory_ptr() with the respective structure, but it doesn't seem to be working.

Code: Select all

Structure MyStructure
  Field1.s
  Field2.i
EndStructure

Define arraySize.i = 2
Dim myArray.MyStructure(arraySize)
Dim memory_ptr.q(arraySize)

For i = 0 To arraySize - 1
  memory_ptr(i) = AllocateMemory(SizeOf(MyStructure))
Next

myArray(0)\Field1 = "Valore1"
myArray(0)\Field2 = 10

myArray(1)\Field1 = "Valore2"
myArray(1)\Field2 = 20

For i = 0 To arraySize - 1
  Debug myArray(i)\Field1
  Debug myArray(i)\Field2
Next

;The problem is here
For i = 0 To arraySize - 1
  *Parameters.MyStructure = memory_ptr(i)
  *Parameters\Field2 = 100
Next i

For i = 0 To arraySize - 1
  Debug myArray(i)\Field1
  Debug myArray(i)\Field2
Next

For i = 0 To arraySize - 1
  FreeMemory(memory_ptr(i))
Next

Re: Problem with pointers to structures.

Posted: Tue Apr 02, 2024 3:18 am
by Quin
That's because you're not actually setting the value on the structure in the array. When you do *Parameters.MyStructure = memory_ptr(i), you're initializing a new structure. You can see this by trying to debug the values on *Parameters :wink:

BTW: You can use AllocateStructure/FreeStructure instead of needing to pass SizeOf(MyStructure) to AllocateMemory. It initializes the structure fields for you too :)

Re: Problem with pointers to structures.

Posted: Tue Apr 02, 2024 4:32 am
by Demivec
Armoured wrote: Tue Apr 02, 2024 12:27 am Could someone help me figure out what's wrong with this code? I can't seem to understand it.
In theory, I should be able to change the values of "Field1" or "Field2" by associating the pointers contained in the array memory_ptr() with the respective structure, but it doesn't seem to be working.
It seemed as if you wanted to modify our work with elements from the existing array myArray() and not create no ones. If that is so then you can do it with the following changes. If you wanted to create new structures then use memory_ptr(i) = AllocateStructure(MyStructure) and clean them up with FreeStructure(memory_ptr(i)).

Code: Select all

Structure MyStructure
  Field1.s
  Field2.i
EndStructure

Define arraySize.i = 2
Dim myArray.MyStructure(arraySize)
Dim memory_ptr.q(arraySize)

For i = 0 To arraySize - 1
  ;memory_ptr(i) = AllocateMemory(SizeOf(MyStructure))
  memory_ptr(i) = @myArray(i)  ;<== point to the existing element not to a new one
Next

myArray(0)\Field1 = "Valore1"
myArray(0)\Field2 = 10

myArray(1)\Field1 = "Valore2"
myArray(1)\Field2 = 20

For i = 0 To arraySize - 1
  Debug myArray(i)\Field1
  Debug myArray(i)\Field2
Next

;The problem is here
For i = 0 To arraySize - 1
  *Parameters.MyStructure = memory_ptr(i)
  *Parameters\Field2 = 100
Next i

For i = 0 To arraySize - 1
  Debug myArray(i)\Field1
  Debug myArray(i)\Field2
Next

;<== the following lines aren't needed because no new structures were allocated
;For i = 0 To arraySize - 1
;  FreeMemory(memory_ptr(i))
;Next

Re: Problem with pointers to structures.

Posted: Tue Apr 02, 2024 11:44 am
by Armoured
Both Quin's and Demivec's examples were helpful to me. I need to create a new structure every time because then I pass them as parameters to different threads. Clearly, I still have a lot to learn about pointers and structures.
This is my version based on your examples:

Code: Select all

Structure MyStructure
  Field1.s
  Field2.i
EndStructure

Define arraySize.i = 2

Dim memory_ptr.q(arraySize)
Define *Parameters.MyStructure

For i = 0 To arraySize - 1
  memory_ptr(i) = AllocateStructure(MyStructure)
  *Parameters = memory_ptr(i)
Next i

*Parameters = memory_ptr(0)
; Assegnamento dei valori ai campi della struttura
*Parameters\Field1 = "Valore1"
*Parameters\Field2 = 10

*Parameters = memory_ptr(1)
*Parameters\Field1 = "Valore2"
*Parameters\Field2 = 10

For i = 0 To arraySize - 1
  *Parameters = memory_ptr(i)
  Debug *Parameters\Field1
  Debug *Parameters\Field2
Next

*Parameters = memory_ptr(1)
*Parameters\Field1 = "Valore3"
*Parameters\Field2 = 100

For i = 0 To arraySize - 1
  *Parameters = memory_ptr(i)
  Debug *Parameters\Field1
  Debug *Parameters\Field2
Next

For i = 0 To arraySize - 1
  FreeStructure(memory_ptr(i))
Next
Thanks again to both of you. :)