In C, I am trying to do the following:
typedef struct {
int length;
int items[]; /* 1 */
} wchararray_t;
typedef struct {
long hash;
wchararray_t chars; /* 2 */
} string_t;
static string_t s1 = {
617862378,
{ 5, { 'H', 'e', 'l', 'l', 'o' } } /* 3 */
};
In full words, I would like a type string_t
that ends in another type wchararray_t
that is itself dynamically sized -- its size being stored in length
. Moreover, I would also like to write a prebuilt particular string, as static data, here s1
of length 5.
The code above assumes C99 support for /* 1 */
. The inclusion of the substructure into the bigger structure at /* 2 */
is, as far as I understand, not supported even by the C99 standard -- but GCC accepts it. However, at /* 3 */
GCC gives up:
error: initialization of flexible array member in a nested context
As a workaround, the ideal code above is so far written as the following hack, which "kind of works":
typedef struct { int length; int items[1]; } wchararray_t;
typedef struct { long hash; wchararray_t chars; } string_t;
typedef struct { int length; int items[5]; } wchararray_len5_t;
typedef struct { long hash; wchararray_len5_t chars; } string_len5_t;
static union { string_len5_t a; string_t b; } s1 = {
617862378,
{ 5, { 'H', 'e', 'l', 'l', 'o' } }
};
...and we'd use "s1.b" as the prebuilt string_t (and never refer to "s1.a", which is here only for the static declaration of s1). However, it breaks in the newest GCC 4.8, which optimizes away parts of our code because -- obviously -- any loop over the items
of a wchararray_t
can iterate only once, given that it is an array of length 1.
This particular issue is fixed by giving gcc the option -fno-aggressive-loop-optimizations
. It can probably also be fixed by not declaring the length in wchararray_t
's items[]
array, making it a dynamic array "just because". However, this way to write code is such a hack that I'd prefer a fully different way to approach the problem...
(Note that it is all generated C code produced by PyPy, as opposed to hand-written code; any change is fine, including if it requires changing the way we access the data everywhere, as long as the "valid" C optimizations are not prevented.)
EDIT: replaced "char[]" with "int[]", which doesn't accept the double-quote syntax "hello"
. This is because I'm looking for a solution for any array type.
NOT RESOLVED: thanks everybody for your suggestions. It seems there is no clean way, so I have implemented the hackish solution: declaring the types k+1 times, once with a flexible array "int items[];" and the k other times with "int items[N];" for the various values of N that are needed. This requires some additional hacks: e.g. not using flexible arrays for MSVC (they work differently there; I didn't investigate to know if exactly the same syntax would work); and GCC follows what C99 says and is not happy with structs that would contain int items[];
as only field. It is however happy if we add a dummy field char _dummy[0];
... which is not strictly C99 as far as I know...
items[]
to be an array ofint
?'H', 'e', 'l', 'l', 'o'
with a list of 5 normal ints, if you prefer.