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 18:09 -0700 |
| Organization | None to speak of |
| Message-ID | <lnh9labhuv.fsf@kst-u.example.com> (permalink) |
| References | (2 earlier) <6408f9ec-e8d2-4526-96ca-271e1d201484@googlegroups.com> <kfn611qdcu2.fsf@x-alumni2.alumni.caltech.edu> <b49266e4-9532-4179-9d4c-fd2c02d7236b@googlegroups.com> <lnlhambmrj.fsf@kst-u.example.com> <kfnoafibkpk.fsf@x-alumni2.alumni.caltech.edu> |
Tim Rentsch <txr@alumni.caltech.edu> writes:
> Keith Thompson <kst-u@mib.org> writes:
>
>> supercat@casperkitty.com writes:
>>> On Wednesday, October 28, 2015 at 2:15:29 PM UTC-5, Tim Rentsch wrote:
>>>> supercat writes:
>>>>>[snip]
>>>
>>> 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; };
[...]
>> No. The standard explicitly says that the layouts must be identical in
>> the second unit. It makes no such guarantee for the first.
>
> I believe it does, indirectly. The struct FOO in TU1 and the
> struct FOO in TU2 are compatible types, so they must use the same
> layout. Similarly for struct GOO's in TU1 and TU2. Therefore
> the two struct types in TU1 must use the same layout, because the
> corresponding compatible types might have to in another (unknown)
> TU that has those corresponding types in a union.
Right. I was thinking of the more general case of two distinct struct
types with a common initial subsequence. I missed the fact that struct
FOO and struct GOO are compatible.
> [snip]
>>> 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.
>>
>> [snip]
>
> Note that there was a change in this area between C99 and N1256.
> It might be useful to compare this section in the two documents.
The change was made in response to Defect Report #282:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_282.htm
Summary:
6.7.2.1 Structure and union specifiers, paragraphs 15 and 16 require
that any padding for alignment of a structure containing a flexible
array member must preceed the flexible array member. This
contradicts existing implementations. We do not believe this was
the intent of the C99 specification.
The original wording in C99 was:
As a special case, the last element of a structure with more
than one named member may have an incomplete array type;
this is called a flexible array member. With two exceptions,
the flexible array member is ignored. First, the size of the
structure shall be equal to the offset of the last element of an
otherwise identical structure that replaces the flexible array
member with an array of unspecified length. Second, when
a . (or ->) operator has a left operand that is (a pointer to)
a structure with a flexible array member and the right operand
names that member, it behaves as if that member were replaced
with the longest array (with the same element type) that would
not make the structure larger than the object being accessed;
the offset of the array shall remain that of the flexible array
member, even if this would differ from that of the replacement
array. If this array would have no elements, it behaves as if it
had one element but the behavior is undefined if any attempt is
made to access that element or to generate a pointer one past it.
with a footnote:
The length is unspecified to allow for the fact that implementations
may give array members different alignments according to their
lengths.
The revised wording in N1256 was:
As a special case, the last element of a structure with more
than one named member may have an incomplete array type;
this is called a flexible array member. In most situations,
the flexible array member is ignored. 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. However, when a . (or ->) operator
has a left operand that is (a pointer to) a structure with a
flexible array member and the right operand names that member,
it behaves as if that member were replaced with the longest
array (with the same element type) that would not make the
structure larger than the object being accessed; the offset of
the array shall remain that of the flexible array member, even
if this would differ from that of the replacement array. If
this array would have no elements, it behaves as if it had one
element but the behavior is undefined if any attempt is made
to access that element or to generate a pointer one past it.
and the footnote was removed.
The same wording appears without change in N1570.
--
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