From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Ian Pilcher <arequipeno@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Flexible array member initializers
Date: Wed, 08 Jan 2014 21:43:00 -0000 [thread overview]
Message-ID: <CAH6eHdQuGyW+T4yjpyjNHbi6akf9xfHezSHqvwnqF7NgDaE6nw@mail.gmail.com> (raw)
In-Reply-To: <52CDB80F.2080807@gmail.com>
On 8 January 2014 20:41, Ian Pilcher wrote:
> On 01/08/2014 03:59 AM, Jonathan Wakely wrote:
>>
>> The member def_value has no storage, so you can't initialize it. To
>> use it you need to allocate sizeof(struct foo_opt)+sizeof(x) where x
>> is the value you want def_val to have, and when you access def_val you
>> will be accessing the extra sizeof(x) bytes after the struct.
>>
>> I don't see how you can statically initialize a foo_opt when you don't
>> know sizeof(x).
>>
>
> Here's a very simplified example for the specific case of a 16-bit short
> on a little-endian system:
>
> #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?
next prev parent reply other threads:[~2014-01-08 21:43 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-08 5:13 Ian Pilcher
2014-01-08 8:59 ` Jonathan Wakely
2014-01-08 20:41 ` Ian Pilcher
2014-01-08 21:00 ` Marc Glisse
2014-01-08 21:46 ` Ian Pilcher
2014-01-08 21:43 ` Jonathan Wakely [this message]
2014-01-08 21:49 ` Ian Pilcher
2014-01-08 22:21 ` Jonathan Wakely
2014-01-08 23:23 ` Ángel González
2014-01-11 2:03 ` Ian Pilcher
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAH6eHdQuGyW+T4yjpyjNHbi6akf9xfHezSHqvwnqF7NgDaE6nw@mail.gmail.com \
--to=jwakely.gcc@gmail.com \
--cc=arequipeno@gmail.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).