Page 1 of 1

Allocating a pointer to an array of structures?

Posted: Sun Sep 29, 2019 3:36 am
by Mistrel
Maybe I'm just out of practice with PureBasic. I don't understand why this isn't working.

This works fine:

Code: Select all

Structure Triangle1
  POINT.POINT[3]
EndStructure

Define *Triangle1.Triangle1

;/ Works fine
*Triangle1=AllocateMemory(SizeOf(POINT)*3)
*Triangle1\POINT[0]\x=0
*Triangle1\POINT[0]\y=0
*Triangle1\POINT[1]\x=0
*Triangle1\POINT[1]\y=0
*Triangle1\POINT[2]\x=0
*Triangle1\POINT[2]\y=0
But this throws a null pointer error:

Code: Select all

Structure Triangle2
  *POINT.POINT[3]
EndStructure

Define Triangle2.Triangle2

Triangle2\POINT=AllocateMemory(SizeOf(POINT)*3)

Triangle2\POINT[0]\x=0
Triangle2\POINT[0]\y=0

;/ ERROR: Pointer is null
Triangle2\POINT[1]\x=1
Triangle2\POINT[1]\y=1
Triangle2\POINT[2]\x=2
Triangle2\POINT[2]\y=2

Re: Allocating a pointer to an array of structures?

Posted: Sun Sep 29, 2019 4:30 am
by Demivec
Mistrel wrote:Maybe I've just out of practice with PureBasic. I don't understand why this isn't working.

Code: Select all

Structure Triangle2
  *POINT.POINT[3]  ;<<< Array of pointers
EndStructure

Define Triangle2.Triangle2

Triangle2\POINT[0]=AllocateMemory(SizeOf(POINT))
Triangle2\POINT[1]=AllocateMemory(SizeOf(POINT))
Triangle2\POINT[2]=AllocateMemory(SizeOf(POINT))

Triangle2\POINT[0]\x=$1
Triangle2\POINT[0]\y=$2
Triangle2\POINT[1]\x=$11
Triangle2\POINT[1]\y=$12
Triangle2\POINT[2]\x=$21
Triangle2\POINT[2]\y=$22

ShowMemoryViewer(Triangle2\Point[0], 16)
CallDebugger
ShowMemoryViewer(Triangle2\Point[1], 16)
CallDebugger
ShowMemoryViewer(Triangle2\Point[2], 16)
CallDebugger

Re: Allocating a pointer to an array of structures?

Posted: Sun Sep 29, 2019 4:49 am
by Mistrel
I think the confusion lies in that *POINT.POINT[3] is actually this:

Code: Select all

Define.POINT[3] *POINT
Or in the structure format:

Code: Select all

*POINT[3].POINT
Note that the above code examples are not legal syntax.

I read *POINT.POINT[3] as a singular pointer to a contiguous array of structures instead of the actual implementation of a contiguous array of pointers to singular structures.

I'm pretty sure I've tripped over this before. It just doesn't seem right.

If I want to access a contiguous array in memory then I have to do this, which is annoying:

Code: Select all

Structure POINT_ARRAY
  Deref.POINT[0]
EndStructure

Structure Triangle
  *POINT.POINT_ARRAY
EndStructure

Define Triangle.Triangle

;/ Works fine
Triangle\POINT=AllocateMemory(SizeOf(POINT)*3)
Triangle\POINT\Deref[0]\x=0
Triangle\POINT\Deref[0]\y=0
Triangle\POINT\Deref[1]\x=0
Triangle\POINT\Deref[1]\y=0
Triangle\POINT\Deref[2]\x=0
Triangle\POINT\Deref[2]\y=0

;/ Unchecked buffer overflow
Triangle\POINT\Deref[3]\y=0

Re: Allocating a pointer to an array of structures?

Posted: Sun Sep 29, 2019 9:54 am
by mk-soft
With the conversion from 'C' to 'PB' I also have problems sometimes.

But a *pointer in the structure is also a pointer for 'C'.
The problem is the shortened spelling.

At the moment I don't know how to write a pointer in 'C' to an array of pointers with structure.

Re: Allocating a pointer to an array of structures?

Posted: Sun Sep 29, 2019 2:51 pm
by Mistrel
I think what I'm grasping for here is a pointer-to-a-pointer which in PureBasic means a pointer to a structure of a pointer. This is why we require the additional structure to dereference the type. And the only way we can dereference a pointer is by accessing a member from a structure.

So, for a pointer to a pointer we need to dereference twice; this means we wold need two structures when done in PureBasic.