> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
> Basile STARYNKEVITCH
> Sent: Wednesday, January 24, 2007 10:30 AM
> To: gcc@gcc.gnu.org
> Subject: variable-sized array fields in structure?
> 
> Hello all,
> 
> It is common to have structures which end with an "undefined"
> variable-length array like
> 
> struct foo_st {
>   struct bar_st* someptr;
>   int len;
>   struct biz_st *tab[] /* actual size is len */;
> };
> 
> I'm sorry to be unable to get the exact wording of this construct,
> which I am sure is in some recent (C99? maybe) standard, unfortunately
> I don't have these standards at hand.

It is discussed in section 6.7.2.1 of the C99 standard, in the Semantics 
section, paragraph #15 that explicitly allows the last element of a structure 
to have an array with no bound, called a flexible array.

I don't have an online version of C90, but it may have been in there as well.

> There is even a length attribute in  GTY to help support this
> http://gcc.gnu.org/onlinedocs/gccint/GTY-Options.html
> 
> I believe the correct idiom in GCC source is to put an explicit
> dimension to 1 (probably because 0 would make some old compilers
> unhappy), ie to code instead
> 
> struct foo_st {
>   struct bar_st* someptr;
>   int len;
>   struct biz_st *tab[1] /* 1 is dummy, actual size is len */;
> };

Pre-ANSI/ISO compilers did not allow this, and 1 was used quite heavily in the 
community for things like char name[1];.
 
> Unfortunately, when debugging (or taking sizeof), this makes a little
> difference.
> 
> My small suggestion would be
> 
> 1. To explicitly document that such undefined variable-sized array
> fields should be declared of dimension VARYING_SIZE (or some other
> word), i.e. to code
> 
>   struct foo_st {
>     struct bar_st* someptr;
>     int len;
>     struct biz_st *tab[VARYING_SIZE] /* actual size is len */;
>   };
> 
> 2. To have a definition of VARYING_SIZE is some of our header files
> (config.h, or system.h or others) which is 1 for old compilers and
> empty for new ones (including gcc itself), maybe
> 
>   #if (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
>   #define VARYING_SIZE 1
>   #else
>   #define VARYING_SIZE /*empty*/
>   #endif

Probably reasonable.
 
> 
> 
> Is there some point that I forgot? Probably yes, since my suggestion
> is quite obvious but not yet in GCC?
> 
> Thanks for reading.
> 
> --
> Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
> email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
> 8, rue de la Faïencerie, 92340 Bourg La Reine, France
> *** opinions {are only mines, sont seulement les miennes} ***
> 


--
Michael Meissner
AMD, MS 83-29
90 Central Street
Boxborough, MA 01719



Reply via email to