Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Small Variant Records in C |
| Date | 2011-12-18 18:35 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jcltbr$pe8$1@dont-email.me> (permalink) |
| References | <126812ae-e278-4572-900f-6068c7fd89ff@d17g2000yql.googlegroups.com> <slrnjeseuh.51v.usenet-nospam@guild.seebs.net> <jclgp9$d9r$1@dont-email.me> <cd16434c-8c43-4b2f-b293-b12ec9bc8d6f@o7g2000yqk.googlegroups.com> |
On 12/18/2011 3:23 PM, Marco Devillers wrote:
> > [...]
>> Since the actual
>> instances you're passing are (probably) smaller and (possibly) not
>> aligned tightly enough for a `double', you're in trouble.
>
> Yah, I see that, thanks for pointing it out. I think I am fine as long
> as I can just cast between the generic and specific variants.
Perhaps "I see that" was premature, or just over-optimistic.
My point is that you *cannot* do the cast safely. Sure, it
looks safe -- but it isn't. I warned you not to ask how I learned
of this pitfall, but it looks like you need the story ...
As in your situation, the program was dealing with type/value
pairs. And it used the struct-for-each-kind dodge, with the type
code (and some other flags) at the same spot in each. And it put
all those structs into a union, and wrote a function that took a
union pointer, switched on the common type code, and didn't even
look at the type-specific parts until it got to the individual
switch cases.
Safe as houses -- in Joplin, Missouri.
Along came a compiler that was fairly aggressive (for its time;
this was more than fifteen years ago, and compilers have grown even
more devious since), and this compiler said to itself "Aha! I've got
a pointer to one of these union things, so there's an entire union
instance where it points. My data flow analysis shows that several
of the cases I'm about to switch between (not all, but several) will
want the doubleword at offset 8, and a few more will want the data
at offset 16, too. I'll save a few instructions by issuing prefetches
for those doublewords up at the top of the function, before the
switch. Ain't I the clever one?"
... and all was well, because if the type code took us to one
of the cases that didn't use the data, it just got prefetched and
ignored; no harm done.
... until the day an incomplete union instance just happened to
butt right up against the end of mapped memory, and the prefetches
took SIGSEGV's.
It took a while to unravel this one, in large part because it
was so hard to reproduce: The memory map had to look *just so* to
trigger it. Once it was understood, though, we saw we would also
have been in alignment trouble on a less forgiving CPU (this one
was taking a slow hardware-assisted fixup on half our instances;
another might have tossed SIGBUS instead).
Once again: The cast is *not* safe. In the immortal words: "If
you lie to the compiler, it will get its revenge."
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 10:16 -0800
Re: Small Variant Records in C Seebs <usenet-nospam@seebs.net> - 2011-12-18 18:50 +0000
Re: Small Variant Records in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-18 15:00 -0500
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 12:23 -0800
Re: Small Variant Records in C Seebs <usenet-nospam@seebs.net> - 2011-12-18 21:15 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 14:13 -0800
Re: Small Variant Records in C Seebs <usenet-nospam@seebs.net> - 2011-12-19 00:26 +0000
Re: Small Variant Records in C "BartC" <bc@freeuk.com> - 2011-12-19 02:25 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 19:08 -0800
Re: Small Variant Records in C "BartC" <bc@freeuk.com> - 2011-12-19 17:24 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-19 11:43 -0800
Re: Small Variant Records in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-19 15:21 -0500
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-19 13:38 -0800
Re: Small Variant Records in C James Kuyper <jameskuyper@verizon.net> - 2011-12-19 16:48 -0500
Re: Small Variant Records in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-19 17:19 -0500
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-19 15:14 -0800
Re: Small Variant Records in C "BartC" <bc@freeuk.com> - 2011-12-19 21:44 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-19 14:02 -0800
Re: Small Variant Records in C Kaz Kylheku <kaz@kylheku.com> - 2011-12-19 18:38 +0000
Re: Small Variant Records in C jgk@panix.com (Joe keane) - 2012-01-04 03:52 +0000
Re: Small Variant Records in C Ian Collins <ian-news@hotmail.com> - 2012-01-04 20:25 +1300
Re: Small Variant Records in C Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 15:53 -0800
Re: Small Variant Records in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-18 18:35 -0500
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 16:01 -0800
Re: Small Variant Records in C Seebs <usenet-nospam@seebs.net> - 2011-12-19 00:26 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 17:02 -0800
Re: Small Variant Records in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-18 19:53 -0500
Re: Small Variant Records in C "BartC" <bc@freeuk.com> - 2011-12-19 01:36 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 18:21 -0800
Re: Small Variant Records in C "BartC" <bc@freeuk.com> - 2011-12-19 02:52 +0000
Re: Small Variant Records in C Marco Devillers <marco.devillers@gmail.com> - 2011-12-18 19:17 -0800
Re: Small Variant Records in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-19 09:24 -0500
Re: Small Variant Records in C "BartC" <bc@freeuk.com> - 2011-12-19 17:18 +0000
csiph-web