This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Flexible array member initializers
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Ian Pilcher <arequipeno at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 8 Jan 2014 22:21:20 +0000
- Subject: Re: Flexible array member initializers
- Authentication-results: sourceware.org; auth=none
- References: <laimok$i1$1 at ger dot gmane dot org> <CAH6eHdTFnTYHLokRdGQ=ErFF=BubM5_aVb-vPhmGoWGd+YxWug at mail dot gmail dot com> <52CDB80F dot 2080807 at gmail dot com> <CAH6eHdQuGyW+T4yjpyjNHbi6akf9xfHezSHqvwnqF7NgDaE6nw at mail dot gmail dot com> <52CDC7C8 dot 5070804 at gmail dot com>
On 8 January 2014 21:48, Ian Pilcher wrote:
> On 01/08/2014 04:42 PM, Jonathan Wakely wrote:
>>>
>>> #define SHORT_BYTES(s) { ((s) & 0xff), (((s) >> 8) & 0xff) }
>>>
>>> struct foo {
>>> size_t value_size;
>>> unsigned char value[];
>>> };
>>>
>>> static struct foo foo_short = {
>>> .value_size = sizeof(short),
>>> .value = SHORT_BYTES(513),
>>> };
>>
>>
>> Where do you think those two bytes are meant to be stored?
>>
>
> Maybe I don't understand your question, but the two bytes that make up
> the short get stored in the "value" element (as an array of 2 unsigned
> characters).
The point of my question is that the 'value' member is an incomplete
type, it does not have any storage, so there is nowhere to store those
two bytes.
But I didn't realise GCC has a non-standard extension that does allow
what you're doing, automatically allocating space after the struct:
http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html