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 19:53 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jcm1v5$mfa$1@dont-email.me> (permalink) |
| References | (1 earlier) <slrnjeseuh.51v.usenet-nospam@guild.seebs.net> <jclgp9$d9r$1@dont-email.me> <cd16434c-8c43-4b2f-b293-b12ec9bc8d6f@o7g2000yqk.googlegroups.com> <jcltbr$pe8$1@dont-email.me> <59e2f8be-ff38-4ca1-ac7e-ce07c3018000@g41g2000yqa.googlegroups.com> |
On 12/18/2011 7:01 PM, Marco Devillers wrote:
>
> What about solving it like this:
>
> struct t_base { int type };
> struct t_int { int type; int value };
> struct t_float { int type; float value };
>
> And passing around *t_base values instead of union values? C99 should
> guarantee that the records are layed out similarly (if I got that
> section/comment right,) and you can now only 'widen' after you
> established the type...
Yes, that's the way to do it. As Seebs points out there are
still aliasing issues, but in the anticipated use cases you won't
encounter them. Not unless you do something awful like
void coerce_to_float(struct t_base *ptr) {
switch (ptr->type) {
case INT:
// t_int and t_float are the same size, so
// I can just do this in place, mwa-ha-haa!
ptr->type = FLOAT;
((struct t_float*)ptr)->value =
((struct t_int*)ptr)->value;
break
...
... in which case all I can say is that you're working very hard
to create trouble for yourself, and may eventually succeed.
By the way, you're not relying on the "similar layout" thing
here, not the one that's special-cased for unions of structs. You
depend on the "pointer to struct is convertible to/from pointer to
first member" guarantee, which applies to all structs whether in
unions or not. In this case it's the `type' field that's relevant;
if you need more goodies you need to put them in their own struct:
struct t_base {
int type;
short coercion_class;
short equivalence_domain;
};
struct t_int {
struct t_base base; // *not* three members!
int value;
};
struct t_float {
struct t_base base; // *not* three members!
float value;
};
This level of squeaky-cleanliness may seem obsessive, but
remember: RAM is getting slower every year (in comparison to CPU),
and compilers are getting more and more inventive about avoiding
RAM accesses in creative ways. Moderately dingy code that works
today may fail on the next compiler, or the next machine -- my tale
of woe happened on just one platform out of the seven our code ran
on at the time. Squeaky-clean is your defense against the future.
--
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