Please add support for undocumented '[0]' in structures.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Please add support for undocumented '[0]' in structures.

Post by Mistrel »

Being able to do this is extremely useful when transversing contiguous blocks of unknown size:

Code: Select all

Structure Struct
  *Field.Type[0]
EndStructure
The problem is that PureBasic doesn't recognize the size of the field in SizeOf(). I'm assuming that AllocateStructure() won't work either but I have no way to tell.

This leads to some interesting results if caught unprepared:

Code: Select all

Structure PolygonXY_1
  *Point.PointXY[0]
  Size.l
EndStructure

Structure PolygonXY_2
  Size.l
  *Point.PointXY[0]
EndStructure

;Debug SizeOf(PolygonXY_1) ;/ 4
;Debug SizeOf(PolygonXY_2) ;/ 4

;Debug OffsetOf(PolygonXY_1\Size);/ 0
;Debug OffsetOf(PolygonXY_2\Size);/ 0

Define Polygon1.PolygonXY_1
Polygon1\Point=1
Polygon1\Size=2

Debug Polygon1\Point ;/ 2
Debug Polygon1\Size  ;/ 2

Define Polygon2.PolygonXY_2
Polygon2\Point=1
Polygon2\Size=2 ;/ <- Overwrites memory?

Debug Polygon2\Point ;/ 1
Debug Polygon2\Size  ;/ 2
Padding needs to be added in this case:

Code: Select all

Structure Something
  *Field1.Type[0]
  _padding.i
  Field2.l
EndStructure
This is confusing to read and adds junk to autocomplete. I would really like to see this added officially or at least have this issue fixed if it's to remain undocumented. Either way, please don't remove it as it's still very useful.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Please add support for undocumented '[0]' in structures.

Post by #NULL »

Padding needs to be added in this case: ...
What's the difference to just using *Field1.Type[1]?
Do you want to emulate some kind of Union, with *Field2, *Field3 etc.?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Please add support for undocumented '[0]' in structures.

Post by Mistrel »

An array with a size of [0] allows you create a variable-length array without having to define an upper-bounds. This is useful when you want to access an array for which you won't know the size of until runtime.

Example:

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
This is commonly an undefined feature of many C and C++ compilers. Using '[1]' was defined but '[0]' also provided more information about intent. Variable-length arrays were later standardized in C99 as Flexible Arrays, albeit with a slightly different syntax.

There is a good summary here:

https://stackoverflow.com/a/9722863/1762276

We can't use '[1]' in PureBasic because, unlike C, it does bounds checking on access.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please add support for undocumented '[0]' in structures.

Post by Little John »

Mistrel wrote:Please add support for undocumented '[0]' in structures.
Sorry, I don't understand your aim.
What kind of support exactly do you want for [0] in structures?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Please add support for undocumented '[0]' in structures.

Post by Josh »

Mistrel wrote:An array with a size of [0] allows you create a variable-length array without having to define an upper-bounds.
With [0] you create an static array with 0 elements !!!!!!



Please Mistrel,

if you have no idea about something and are obviously unwilling to understand it, don't keep opening new threads on the same topic. For this topic, this is the third thread in a few hours:

In the coding questions
In the bug reports
sorry for my bad english
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Please add support for undocumented '[0]' in structures.

Post by #NULL »

I guess the support Mistrel wants is to be able to rely on this, i.e. it being documented (PBs equivalent to a specification). I wonder if the use case of taking and using the pointer to an array is supported in the first place? The array might move in memory on a redim?
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Please add support for undocumented '[0]' in structures.

Post by #NULL »

#NULL wrote:The array might move in memory on a redim?
It seems it does:

Code: Select all

Dim a1.i(10)
Dim a2.i(10)

Debug @ a1() ; 31392168
;Debug @ a2()

*p.Integer = @ a1()
a1(0) = 111
Debug a1(0)  ; 111
Debug *p\i   ; 111

ReDim a1(100000)
Debug @ a1() ; 140619073630280 (array address changed)
;Debug @ a2()

a1(0) * 100
Debug a1(0)  ; 11100 (array element modified)
Debug *p\i   ; 111   (old memory, unchanged)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Please add support for undocumented '[0]' in structures.

Post by wilbert »

#NULL wrote:I guess the support Mistrel wants is to be able to rely on this, i.e. it being documented (PBs equivalent to a specification).
It is important to be able to rely on this.
If you need to interact with a C library, it's not uncommon that an API function returns an array with a variable amount of items.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Please add support for undocumented '[0]' in structures.

Post by mk-soft »

This is the documentation in the help somewhat sparingly...

Code: Select all

;
Structure MyPoint
  x.i
  y.i
EndStructure

; Array of Pointer to structure of point with unknown count of pointers
Structure StructPointerArray
  *Point.MyPoint[0]
EndStructure

; Array point data with unknown count of point data
Structure StructDataArray
  Point.MyPoint[0]
EndStructure
My English ... :?
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: Please add support for undocumented '[0]' in structures.

Post by Mistrel »

#NULL wrote:I guess the support Mistrel wants is to be able to rely on this, i.e. it being documented (PBs equivalent to a specification).
Correct. But I'm also asking for better support with regards to SizeOf().
#NULL wrote:I wonder if the use case of taking and using the pointer to an array is supported in the first place? The array might move in memory on a redim?
I'm not sure. This was just an example. And even if it was a block of memory created with AllocateMemory(), unless it's a pointer to a pointer then the address would still change. The current implementation is expected but I believe it is undocumented as well.
Josh wrote:With [0] you create an static array with 0 elements !!!!!!
I think Josh is right on this and I'm misunderstanding this. Field[0] is a static array; that is, it's a pointer to the first element in a contiguous block of memory for which is defined as having 0 elements. This is probably why [1] was the preferred way in C and C99 adopted '[]' instead. 0 elements is literally 0 elements.

I think this comes down to what the definition is for the ambiguous statement of a "pointer to an array". It could be a pointer that points to the first element of an array or a pointer which is the first element itself. Hhmm...

In this case, '[0]' is a "pointer to the first element of an array". Alternatively, we can only point to a structure whose member is the first element of an array; essentially a pointer to a structure whose member can be accessed as an array.

PureBasic has a lot in common with C but some things which may seem analogous are not.

This was pulled from a Wikipedia entry on sizeof() from C but is a pretty good summary:
sizeof can only be applied to "completely" defined types. With arrays, this means that the dimensions of the array must be present in its declaration, and that the type of the elements must be completely defined. For structs and unions, this means that there must be a member list of completely defined types.
Last edited by Mistrel on Sun Sep 29, 2019 2:26 pm, edited 4 times in total.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please add support for undocumented '[0]' in structures.

Post by Little John »

#NULL wrote:I guess the support Mistrel wants is to be able to rely on this, i.e. it being documented
I agree that official documentation of the meaning of [0] in structures would be good.
mk-soft wrote:This is the documentation in the help somewhat sparingly...
I also searched the PB documentation, but couldn't find anything about [0] in structures.
Where exactly did you find the example code that you posted?
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please add support for undocumented '[0]' in structures.

Post by Little John »

Mistrel wrote:But I'm also asking for better support with regards to SizeOf().
Josh wrote:With [0] you create an static array with 0 elements !!!!!!
SizeOf() is supported, and it consequently returns 0:

Code: Select all

Structure Struct0
  *Field.Type[0]
EndStructure

Structure Struct1
  *Field.Type[1]
EndStructure

Debug SizeOf(Struct0)  ; = 0
Debug SizeOf(Struct1)  ; = 8 (on x64)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Please add support for undocumented '[0]' in structures.

Post by Mistrel »

Little John wrote:Where exactly did you find the example code that you posted?
Considering its similarity to my example, I think he wrote it himself.

Little John, I'm not sure if you missed my last reply. I posted at the same time you did.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please add support for undocumented '[0]' in structures.

Post by Little John »

Mistrel wrote:Considering its similarity to my example, I think he wrote it himself.
Well ...
mk-soft wrote:This is the documentation in the help
Mistrel wrote:Little John, I'm not sure if you missed my last reply. I posted at the same time you did.
No, I did not miss your last reply, but I quoted from it. :-)
Last edited by Little John on Sun Sep 29, 2019 2:16 pm, edited 1 time in total.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Please add support for undocumented '[0]' in structures.

Post by Mistrel »

I just read this in the documentation for Dim:
Note: Array bound checking is only done when the runtime Debugger is enabled.
Unsurprisingly, this works for static arrays as well:

Code: Select all

Structure IntegerArray
  ToArray.i[1]
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
  DisableDebugger
  a=*Pointer\ToArray[i]
  EnableDebugger
  
  Debug a
Next
This is pretty much identical to how C does it. And here again we have the same behavior: [0] is an empty set whose behavior is abused, and [1] is ideal but ambiguous-- is it an array of 1 or a VLA?

Maybe Fred can complete the circle by implementing '[]' to define VLA. The behavior would be akin to [1] but without the ambiguity and would only make sense when used at the tail end of a a structure.
Post Reply