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