Page 1 of 1
Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 3:20 am
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].
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 4:37 am
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
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 6:07 am
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.
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 6:10 am
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
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 7:32 am
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.
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 7:57 am
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.
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 8:12 am
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
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 8:47 am
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.
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 9:27 am
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)
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 9:36 am
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.
Re: Pointer to an array in structures incorrectly sized
Posted: Sun Sep 29, 2019 9:44 am
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.