Re: [AD] Proposal: removal of zero-sized arrays

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


> I've tried to understand [#15] a number of
> times this week, but I can't read out from it that
> static initialisation is undefined, Eric.

I think any initialization, static or not, of flexible array members is
undefined.

> Probably my Enklisch is too poor, but to me it
> speaks about the behaviovior of a certain instance of
> a struct with a flexible array member, that happens
> to have no elements (and it says that it is in that
> case undefined when accesing obj.arr[0]).
> ..and also specifies the value sizeof(struct flex)
> ..and that obj.arr[i] works fine if i < "number
> of elements" of the instance.

I think the second part of [#15] ("When an lvalue...") simply means that
flexible array members behave "the right way", i.e you can access the array
appended to the structure with s.arr[i], provided that i is not too big.
More precisely, given the definition

[3.15] Object
[#1] A region of data storage in the execution  environment,
       the  contents  of  which  can  represent values.  Except for
       bit-fields, objects are composed of contiguous sequences  of
       one  or more bytes, the number, order, and encoding of which
       are either explicitly specified  or  implementation-defined.
       When  referenced,  an  object may be interpreted as having a
       particular type; see 6.2.2.1.

it says that if you define your object by:

    struct s { ...; char arr[]; } *obj;

    obj = (struct s *) malloc(sizeof(struct s) + N);

you can access the array with obj.arr[i] provided that 0 <= i <= N-1.

> I haven't either found somewhere else anything that
> indicates that static  initialisation should be
> undefined.

It's not marked as "undefined behaviour". But in light of:

[6.5.8]
[#19] Otherwise, the initializer  for  an  object  that  has
       aggregate   type   shall   be   a   brace-enclosed  list  of
       initializers for the named members of the aggregate, written
       in increasing subscript or member order; and the initializer
       for an object that has union type shall be a  brace-enclosed
       initializer for the first named member of the union.

[#22] If an array of unknown size is initialized,  its  size
       is  determined  by  the  largest  indexed  element  with  an
       explicit initializer.  At the end of its  initializer  list,
       the array no longer has incomplete type.

[#2] No initializer shall attempt to provide a value for  an
       object not contained within the entity being initialized.


there is certainly a problem.

--
Eric Botcazou



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/