Allocating a pointer to an array of structures?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Allocating a pointer to an array of structures?

Post 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
Last edited by Mistrel on Sun Sep 29, 2019 6:09 am, edited 1 time in total.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Allocating a pointer to an array of structures?

Post 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
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allocating a pointer to an array of structures?

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 5401
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Allocating a pointer to an array of structures?

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allocating a pointer to an array of structures?

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