Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How does strict aliasing work with parallel arrays in a union or const-initialized data |
| Date | 2015-10-28 16:23 -0700 |
| Organization | None to speak of |
| Message-ID | <lnlhambmrj.fsf@kst-u.example.com> (permalink) |
| References | <44332515-f0c2-40f4-b435-c1351188e3bc@googlegroups.com> <kfn4mhfefyv.fsf@x-alumni2.alumni.caltech.edu> <6408f9ec-e8d2-4526-96ca-271e1d201484@googlegroups.com> <kfn611qdcu2.fsf@x-alumni2.alumni.caltech.edu> <b49266e4-9532-4179-9d4c-fd2c02d7236b@googlegroups.com> |
supercat@casperkitty.com writes:
> On Wednesday, October 28, 2015 at 2:15:29 PM UTC-5, Tim Rentsch wrote:
>> supercat writes:
>> > Is there anything specific in the standard that makes clear that
>> > thing.ww[0] = 0x12341234; is processed as a write to "thing", rather
>> > than regarding "thing.ww" as an expression which yields a pointer
>> > which is then offset and dereferenced without reference to the object
>> > of which it had been a part?
>>
>> There is no way to give an objective answer to this question.
>> What is it that are you trying to find out?
>
> I guess I should start by recognizing that C99 and C11 have rules that
> define things differently; I'd like to have code be compatible with both
> when practical, though it would be useful to know in what ways C11 fixes
> shortcomings of the C99 rules, and to what extent it adds shortcomings
> of its own.
You assert that the rules in C11 differ from the rules in C99. In the
area of "strict aliasing" and/or effective type rules, I don't believe
that's correct, though I could easily be missing something. What
differences do you think there are?
[...]
> If I have one translation unit which contains the definitions:
>
> struct FOO { int x,y; char b[]; };
> struct GOO { int x,y; char b[4]; };
>
> and another that contains those two definitions plus
>
> union FOOGOO { FOO f; GOO g; };
>
> the layout of the FOO in both translation units must be identical, the
> layout of GOO in both translation units must be identical. Since the
> layouts of common initial sequences of structure types FOO and GOO in
> the second unit must be identical, would that not imply the same about
> those in the first?
No. The standard explicitly says that the layouts must be identical in
the second unit. It makes no such guarantee for the first.
The easiest way for a compiler to satisfy the standard's requirements
would be to make the layouts identical in either case, but the standard
doen't require this.
>> What makes you think C99 (or later) does not allow declaring
>> of struct-wth-FAM objects, either as ordinary identfiers or
>> as struct/union members? AFAIK they are allowed. When I tried
>> compiling a simple test case with gcc, using -std=c99 -pedantic,
>> no diagnostics were given. So I'm at a loss to understand your
>> question here.
>
> The Keil ARM compiler version 5 has a "C99" and GCC modes. Without C99
> mode enabled, it won't accept flexible-array member syntax at all.
> With C99 mode, it accepts flexible-array member syntax, but unless
> GCC mode is enabled a struct containing a flexible array member may not
> be included as part of a union nor as anything other than the last member
> of another struct [in which case the latter struct is treated as having a
> flexible array member].
If it accepts a structure containing a structure containing a flexible
array member, even as the last member, it sounds like it's being overly
permissive, since that would be a constraint violation (N1570
6.7.2.1p3).
An example:
struct inner { int len; unsigned char fam[]; };
struct outer { struct inner inn; };
A conforming C11 compiler must issue a diagnostic. (A conforming C90
compiler also must issue a diagnostic, but for a different reason.)
> The coverage of flexible-array members in N1570 seems pretty
> inadequate; it does not, for example, say that the reported size of a
> structure with a flexible array member as its last element must be at
> least equal to the offset of that member within the structure, though
> I can't see how anyone could be expected to write sane code without
> such a guarantee.
It doesn't explicitly say it, but it appears to me that that was the
assumption:
In particular, the size of the structure is as if the flexible array
member were omitted except that it may have more trailing padding
than the omission would imply.
You can use offsetof() to determine where the flexible array member
starts. For example, if you want to allocate a structure with 42
elements allocated to the FAM, you can do something like:
struct_type *ptr = malloc(offsetof(struct_type, fam) +
42 * sizeof (element_type));
(I think that's right; I haven't checked it.)
> Further, flexible array members certainly were not available in C89 when the
> aliasing rules were first written; by what means could one write code to meet
> the indicated requirement in C89 without going against the aliasing rules?
The semantics of accessing elements of a flexible array member are
described in N1570 6.7.2.1p18. I think the description there is enough
to avoid any aliasing problems. What problem do you see?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-21 16:47 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 21:20 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-25 11:44 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 12:15 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-28 14:25 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-28 16:23 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 17:08 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-28 18:09 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-28 20:02 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Bartc <bc@freeuk.com> - 2015-10-29 10:55 +0000
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-29 06:50 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-29 08:52 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-31 10:21 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-29 08:50 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-29 12:25 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-30 13:02 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-30 13:35 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-30 19:57 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-31 12:27 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-30 17:02 -0400
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-30 14:16 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-31 08:13 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-31 08:42 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-10-31 10:40 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-02 01:45 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-11-02 06:57 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-02 08:12 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-11-02 08:54 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-04 07:24 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-11-04 09:22 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-04 19:48 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-11-05 09:44 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-07 10:17 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:37 +0000
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data supercat@casperkitty.com - 2015-11-10 09:01 -0800
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 16:48 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Keith Thompson <kst-u@mib.org> - 2015-10-28 16:11 -0700
Re: How does strict aliasing work with parallel arrays in a union or const-initialized data Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 16:37 -0700
csiph-web