PureBasic Forum
http://forums.purebasic.com/english/

Please add support for undocumented '[0]' in structures.
http://forums.purebasic.com/english/viewtopic.php?f=3&t=73695
Page 2 of 2

Author:  Mistrel [ Sun Sep 29, 2019 2:30 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

I don't have a working example but I read this while googling:

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

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

Author:  Josh [ Sun Sep 29, 2019 2:59 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

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.

Author:  Mistrel [ Sun Sep 29, 2019 3:04 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

Josh, I've clarified my previous post.

Author:  Josh [ Sun Sep 29, 2019 3:21 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

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:
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]

Author:  skywalk [ Sun Sep 29, 2019 4:41 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

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:
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]

Author:  Josh [ Sun Sep 29, 2019 6:59 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

@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]

Author:  skywalk [ Sun Sep 29, 2019 11:10 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

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.

Author:  Mistrel [ Sun Sep 29, 2019 11:22 pm ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

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


Quote:
@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)


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

Author:  idle [ Mon Sep 30, 2019 12:41 am ]
Post subject:  Re: Please add support for undocumented '[0]' in structures.

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


Page 2 of 2 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/