It is currently Fri Nov 15, 2019 3:59 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 7:41 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3360
Being able to do this is extremely useful when transversing contiguous blocks of unknown size:

Code:
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:
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 7:57 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1131
Location: right here
Quote:
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.?


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 9:23 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3360
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 9:49 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3700
Location: Berlin, Germany
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?

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 10:08 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 922
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 10:44 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1131
Location: right here
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 10:54 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1131
Location: right here
#NULL wrote:
The array might move in memory on a redim?

It seems it does:
Code:
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 11:29 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3517
Location: Netherlands
#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.

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 12:30 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2028
Location: Germany
This is the documentation in the help somewhat sparingly...
Code:
;
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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 1:52 pm 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3360
#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:

Quote:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 1:53 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3700
Location: Berlin, Germany
#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?

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 2:03 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3700
Location: Berlin, Germany
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:
Structure Struct0
  *Field.Type[0]
EndStructure

Structure Struct1
  *Field.Type[1]
EndStructure

Debug SizeOf(Struct0)  ; = 0
Debug SizeOf(Struct1)  ; = 8 (on x64)

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 2:04 pm 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3360
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 2:08 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3700
Location: Berlin, Germany
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. :-)

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Last edited by Little John on Sun Sep 29, 2019 2:16 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Please add support for undocumented '[0]' in structures.
PostPosted: Sun Sep 29, 2019 2:13 pm 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3360
I just read this in the documentation for Dim:

Quote:
Note: Array bound checking is only done when the runtime Debugger is enabled.


Unsurprisingly, this works for static arrays as well:

Code:
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye