It is currently Fri Nov 22, 2019 9:06 pm

All times are UTC + 1 hour




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

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


Last edited by Mistrel on Sun Sep 29, 2019 3:03 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 2:59 pm 
Offline
Addict
Addict
User avatar

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


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

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
Josh, I've clarified my previous post.


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

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

_________________
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 4:41 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3123
Location: Boston, MA
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]

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


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

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


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

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3123
Location: Boston, MA
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


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

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


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

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3408
Location: New Zealand
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   



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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 15 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