Message ID | 20240419184317.2138890-2-qing.zhao@oracle.com |
---|---|
State | New |
Headers | show |
Series | Allow flexible array members in unions and alone in structures [PR53548] | expand |
>>>>> Qing Zhao <qing.zhao@oracle.com> writes: > +The size of the union is as if the flexiable array member were omitted > +except that it may have more trailing padding than the omission would imply. > + > +If all the members of a union are flexiable array member, the size of There's a couple of spots that say "flexiable" which should say "flexible". thanks, Tom
> On Apr 19, 2024, at 16:54, Tom Tromey <tom@tromey.com> wrote: > >>>>>> Qing Zhao <qing.zhao@oracle.com> writes: > >> +The size of the union is as if the flexiable array member were omitted >> +except that it may have more trailing padding than the omission would imply. >> + >> +If all the members of a union are flexiable array member, the size of > > There's a couple of spots that say "flexiable" which should say "flexible". Thanks for catching those typo, I will fix them. Qing > > thanks, > Tom
On Fri, 19 Apr 2024, Qing Zhao wrote: > +The size of the union is as if the flexiable array member were omitted > +except that it may have more trailing padding than the omission would imply. "trailing padding" is more a concept for structures than for unions (where padding depends on which union member is active). But I suppose it's still true that the union can be larger than without the flexible member, because of alignment considerations. union u { char c; int a[]; }; needs to be sufficiently aligned for int, which means the size is a multiple of the size of int, whereas if the flexible array member weren't present, the size could be 1 byte.
> On Apr 23, 2024, at 14:04, Joseph Myers <josmyers@redhat.com> wrote: > > On Fri, 19 Apr 2024, Qing Zhao wrote: > >> +The size of the union is as if the flexiable array member were omitted >> +except that it may have more trailing padding than the omission would imply. > > "trailing padding" is more a concept for structures than for unions (where > padding depends on which union member is active). But I suppose it's > still true that the union can be larger than without the flexible member, > because of alignment considerations. > > union u { char c; int a[]; }; > > needs to be sufficiently aligned for int, which means the size is a > multiple of the size of int, whereas if the flexible array member weren't > present, the size could be 1 byte. Yes, that’s exact what I tried to include in the documentation part -:) And I have a testing case for this in the patch. However, I am not very confident on the wording of the doc, is the current wording good enough for this? Or do you have any suggestion on how to make it better? Thanks a lot! Qing > > -- > Joseph S. Myers > josmyers@redhat.com >
On Tue, 23 Apr 2024, Qing Zhao wrote: > However, I am not very confident on the wording of the doc, is the > current wording good enough for this? Or do you have any suggestion on > how to make it better? I'm not convinced the statement about size (in relation to a structure with the member omitted) is useful for unions the way it is for structures. The structure with the member omitted is a relevant concept for thinking about a structure with a flexible array member (the flexible array member essentially goes after that structure); it's much less relevant for thinking about a union with a flexible array member. (The statement that the size is zero when all members are flexible array members still seems a useful one to make.)
> On Apr 23, 2024, at 15:03, Joseph Myers <josmyers@redhat.com> wrote: > > On Tue, 23 Apr 2024, Qing Zhao wrote: > >> However, I am not very confident on the wording of the doc, is the >> current wording good enough for this? Or do you have any suggestion on >> how to make it better? > > I'm not convinced the statement about size (in relation to a structure > with the member omitted) is useful for unions the way it is for > structures. The structure with the member omitted is a relevant concept > for thinking about a structure with a flexible array member (the flexible > array member essentially goes after that structure); it's much less > relevant for thinking about a union with a flexible array member. Okay, then I will delete that statement about size. > > (The statement that the size is zero when all members are flexible array > members still seems a useful one to make.) And only keep the size is zero when all members are flexible array members. Thanks. Qing > > -- > Joseph S. Myers > josmyers@redhat.com >
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 7b54a241a7bf..b12ce5fb9b87 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -42,6 +42,8 @@ extensions, accepted by GCC in C90 mode and in C++. * Named Address Spaces::Named address spaces. * Zero Length:: Zero-length arrays. * Empty Structures:: Structures with no members. +* Flexible Array Members in Unions:: Unions with Flexible Array Members. +* Flexible Array Members alone in Structures:: Structures with only Flexible Array Members. * Variable Length:: Arrays whose length is computed at run time. * Variadic Macros:: Macros with a variable number of arguments. * Escaped Newlines:: Slightly looser rules for escaped newlines. @@ -1873,6 +1875,41 @@ The structure has size zero. In C++, empty structures are part of the language. G++ treats empty structures as if they had a single member of type @code{char}. +@node Flexible Array Members in Unions +@section Unions with Flexible Array Members +@cindex unions with flexible array members +@cindex unions with FAMs + +GCC permits a C99 flexible array member (FAM) to be in a union: + +@smallexample +union with_fam @{ + int a; + int b[]; +@}; +@end smallexample + +The size of the union is as if the flexiable array member were omitted +except that it may have more trailing padding than the omission would imply. + +If all the members of a union are flexiable array member, the size of +such union is zero. + +@node Flexible Array Members alone in Structures +@section Structures with only Flexible Array Members +@cindex structures with only flexible array members +@cindex structures with only FAMs + +GCC permits a C99 flexible array member (FAM) to be alone in a structure: + +@smallexample +struct only_fam @{ + int b[]; +@}; +@end smallexample + +The size of such structure gives the size zero. + @node Variable Length @section Arrays of Variable Length @cindex variable-length arrays