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

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

Post by Mistrel »

I don't have a working example but I read this while googling:
This was used before C99 as well, but it was then undefined behavior, known as the "struct hack" referred to in another answer. Before C90, there could be unexpected padding bytes at the end of the struct, leading to bugs.
and
Yes, it is impossible to use a VLA in a structure. The size of a structure has to be known at compile time. FAMs are not counted in the size of the structure, though they may affect the amount of padding in the structure.
This makes sense if the structure with an array of [0] includes any member preceding it. It should work regardless (although still undefined) if the static array is the only element of the structure. This seems to be one of the possible sources of bugs when writing in C due to the use of undefined behavior, addressed by using [1] or [] (C99) instead.
Last edited by Mistrel on Sun Sep 29, 2019 3:03 pm, edited 4 times in total.
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:This seems to be one of the "bugs" addressed by using [1] or [] (C99) instead.
Not everything you don't understand is a bug. Please accept that. THERE'S NO MISTAKE AND THERE'S NOTHING THAT NEEDS TO BE DONE DIFFERENTLY. As I wrote before, a static array with a size of [0] creates an array with zero elements and therefore has no size.

To your example in your first post
With Polygon2\Point=1 you write to an unallocated memory and wonder if something unexpected happens.
sorry for my bad english
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 »

Josh, I've clarified my previous post.
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:Josh, I've clarified my previous post.
I don't know what you're trying to say with your two quotes. Perhaps the following example will help you to understand the behavior of [0].

Code: Select all

Structure MYSTRUC
  a1.i
  a2.i
  i.i[0] ; has no size
  a3.i
  a4.i
EndStructure

Define x.MYSTRUC

x\a1 = 1
x\a2 = 2
x\a3 = 3
x\a4 = 4

Debug x\i[-2]
Debug x\i[-1]
Debug x\i[ 0]
Debug x\i[ 1]
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post by skywalk »

I am also having trouble following this request?
What is broken?
Are you asking for documented examples of zero length arrays?
I never use a structure like below, since we can create a standalone structure to walk any piece of memory we desire. And I think it is dangerous to muddy your core structure with unbounded elements. Instead, use Array i.i(0) if you want a variable length array in MYSTRUC.
ReDim x\i(100)

Code: Select all

Structure MYSTRUC
  a1.i
  a2.i
StructureUnion
  i.i[0]
EndStructureUnion
  a3.i
  a4.i
EndStructure
Define x.MYSTRUC
Debug SizeOf(MYSTRUC)
Debug SizeOf(x)
x\a1 = 1
x\a2 = 2
x\a3 = 3
x\a4 = 4
Debug x\i[-10]
Debug x\i[-1]
Debug x\i[ 0]
Debug x\i[ 1]
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
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 »

@Skywalk

I did not say that a code like the one in my example should be used. I chose this example because I think it shows how [0] works. In his first example, Mistrel wrote with [0] wildly in unallocated memory because I don't think he knows what [0] means.
skywalk wrote:Instead, use Array i.i(0) if you want a variable length array in MYSTRUC.
Sorry, an Array has nothing to do what you can do with an static Array of size [0]
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post by skywalk »

That is my question. What does he want to accomplish?
I merely said "instead of" using a static array[0] for variable lengths, use an Array(0 to whatever). :?:
I use many external dll's and none of these issues.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
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 »

skywalk wrote:That is my question. What does he want to accomplish?
I would like to see defined behavior for variable-length arrays in structures.

More interesting things to read:

Using [1] has been shown to cause GCC to generate incorrect code

And:
https://stackoverflow.com/questions/246 ... d-practice

@Remo.D: minor point: the n-1 does not accurately accounts for the extra allocation, because of alignment: on most 32-bit machines, sizeof(struct header) will be 8 (to remain multiple of 4, since it has a 32-bit field which prefers/requires such alignment). The "better" version is: malloc(offsetof(struct header, data) + n)
In C99 using unsigned char data[1] isn't portable because ((header*)ptr)->data + 2 -- even if enough space was allocated -- creates a pointer that points outside the length-1 array object (and not the sentinel one past the end). But per C99 6.5.6p8, "If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined" (emphasis added). Flexible arrays (6.7.2.2p16) act like an array filling the allocated space to not hit UB here.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

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

Post by idle »

it would be good if it worked as you'd expect, though it is what it is.
the key is not to use it directly in your structure, it requires a bit more code and can look messy with nested arrays
but it works as expected

Code: Select all

Structure arPoint 
   itr.Point[0] 
EndStructure 
 
Structure PolygonXY_1
  Size.l
  *Point.arPoint
  color.l  
EndStructure

Debug SizeOf(PolygonXY_1) 
Debug OffsetOf(PolygonXY_1\Size)
Debug OffsetOf(PolygonXY_1\Point) 
Debug OffsetOf(PolygonXY_1\color)

Define Polygon1.PolygonXY_1
Define size=5 
Polygon1\Point = AllocateMemory(SizeOf(Point)*size)
For a = 0 To size
  Polygon1\Point\itr[a]\x=a : Polygon1\Point\itr[a]\y=size-a
  Debug Str(Polygon1\Point\itr[a]\x) + " " + Str(Polygon1\Point\itr[a]\y) 
Next   

Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply