Problem with pointers to structures.

Just starting out? Need help? Post your questions and find answers here.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Problem with pointers to structures.

Post 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
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Problem with pointers to structures.

Post 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 :)
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Problem with pointers to structures.

Post 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
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Problem with pointers to structures.

Post 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. :)
Post Reply