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.

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 */;
};

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



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} ***

Reply via email to