Pointer to an array in structures incorrectly sized

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

Pointer to an array in structures incorrectly sized

Post by Mistrel »

I want to make a structure which contains a pointer to an array of other structures:

Code: Select all

Structure Triangle
  *POINT.POINT[3]
EndStructure
But the size of the structure is SizeOf(Integer)*3 instead of SizeOf(Integer) as expected. The compiler seems to recognize this as no different from *POINT[3].
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Pointer to an array in structures incorrectly sized

Post by Demivec »

Not a bug.

Code: Select all

Structure POINT_3
  POINT.Point[3]
EndStructure

Structure Triangle2
  *POINT.POINT_3
EndStructure
Debug SizeOf (Triangle2\POINT)

Define Triangle2.Triangle2

Triangle2\POINT=AllocateStructure(POINT_3)

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

ShowMemoryViewer(Triangle2\Point, 50)
CallDebugger
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Pointer to an array in structures incorrectly sized

Post by Mistrel »

Not a bug but the syntax is confusing:

viewtopic.php?f=13&t=73694

IMO, *p.struct[n] should be changed in favor of *p[n].struct as it makes more sense.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Pointer to an array in structures incorrectly sized

Post by Josh »

The compiler recognizes this as no different than *POINT[3] because it is no different. A pointer is a pointer, no matter if a structure is assigned to it or not.

If you don't understand something, then you should first try to find the error in your system and not assume an error to Pb.

@Any Mod
Please move this thread to the garbage
sorry for my bad english
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Pointer to an array in structures incorrectly sized

Post by Mistrel »

Josh wrote:The compiler recognizes this as no different than *POINT[3] because it is no different. A pointer is a pointer, no matter if a structure is assigned to it or not.
I understand this. What I'm saying is that the syntax suggest for Field.Type that the array portion belongs to the type and it's confusing. There is a big difference between and array of pointers and a pointer to an array.

If it worked the way I had anticipated then I could do things such as allocating various shapes:

Code: Select all

Structure Triangle
  *Point.PointXY[3]
  Size.l
EndStructure

Structure Rectangle
  *Point.PointXY[4]
  Size.l
EndStructure

Structure Pentagon
  *Point.PointXY[5]
  Size.l
EndStructure
And pass them around without knowing their type and still access them with a Polygon structure:

Code: Select all

Structure PolygonXY
  *Point.PointXY[0]
  Size.l
EndStructure
I'm already aware of ways to work around this; I'm not asking for solutions but instead explaining my intentions.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Pointer to an array in structures incorrectly sized

Post by Demivec »

Mistrel wrote:
Josh wrote:The compiler recognizes this as no different than *POINT[3] because it is no different. A pointer is a pointer, no matter if a structure is assigned to it or not.
I understand this. What I'm saying is that the syntax suggest for Field.Type that the array portion belongs to the type and it's confusing. There is a big difference between and array of pointers and a pointer to an array.
There is no such thing as a pointer to an array. There is only an unstructured pointer to an address or a structured pointer to a defined stucture.

If you want a pointer to an array then you have to define a structure that contains an array.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Pointer to an array in structures incorrectly sized

Post by Mistrel »

Demivec wrote:There is no such thing as a pointer to an array.
Of course there is.

Code: Select all

Structure IntegerArray
  ToArray.i[0]
EndStructure

Dim AnArray(10)

For i=0 To 10-1
  AnArray(i)=i+1
Next i

Define *Pointer.IntegerArray=@AnArray()

For i=0 To 10-1
  Debug *Pointer\ToArray[i]
Next
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Pointer to an array in structures incorrectly sized

Post by Demivec »

Mistrel wrote:
Demivec wrote:There is no such thing as a pointer to an array.
Of course there is.

Code: Select all

Structure IntegerArray
  ToArray.i[0]
EndStructure

Dim AnArray(10)

For i=0 To 10-1
  AnArray(i)=i+1
Next i

Define *Pointer.IntegerArray=@AnArray()

For i=0 To 10-1
  Debug *Pointer\ToArray[i]
Next
You've only shown a structured pointer as I specified.
Demivec wrote:There is no such thing as a pointer to an array. There is only an unstructured pointer to an address or a structured pointer to a defined stucture.

If you want a pointer to an array then you have to define a structure that contains an array.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Pointer to an array in structures incorrectly sized

Post by Mistrel »

Current workaround. Provides correct size and excludes padding entries for autocompletion. Also ugly as hell.

Code: Select all

Structure _ARRAY_OF_POINTS_XY
  PointXY.i[0]
  _padding.i[1]
EndStructure

CompilerIf #False
Structure ARRAY_OF_POINTS_XY
  PointXY.i[0]
EndStructure
Structure ARRAY_OF_POINTS_XY Extends ARRAY_OF_POINTS_XY
EndStructure
CompilerEndIf
Macro ARRAY_OF_POINTS_XY
  _ARRAY_OF_POINTS_XY
EndMacro

;/ 4
Debug SizeOf(ARRAY_OF_POINTS_XY)
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Pointer to an array in structures incorrectly sized

Post by Josh »

Demivec wrote:There is no such thing as a pointer to an array.
I would say the term 'pointer to an array' is a matter of definition. In my opinion the pointer to the first element of an array could be seen as 'pointer to an array'.

In any case, it doesn't change the fact that this is not a Pb bug and the thread belongs moved.
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Pointer to an array in structures incorrectly sized

Post by Demivec »

Josh wrote:
Demivec wrote:There is no such thing as a pointer to an array.
I would say the term 'pointer to an array' is a matter of definition. In my opinion the pointer to the first element of an array could be seen as 'pointer to an array'.
I agree with you at least in regard to definitions.

My point is that PureBasic only has structured and unstructured pointers. The unstructured ones only hold an address (to anything) and perform no other useful function. Structured Pointers hold an address and allow a structure (and its elements) at that address to be dereferenced. However there is no structure defined in PureBasic for an 'Array", 'List' or 'Map' but those things are instead included in another structure (perhaps similar to native types i.e Integer). Even a simple static array of [x] elements needs to be defined in a structure to be accessed with a structured pointer.
Post Reply