Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18540

Re: What is the right way to do this simple thing

From cri@tiac.net (Richard Harter)
Newsgroups comp.lang.c
Subject Re: What is the right way to do this simple thing
Date 2012-03-11 16:51 +0000
Message-ID <4f5cd20b.2021711914@text.giganews.com> (permalink)
References (3 earlier) <9s28jrFf51U2@mid.individual.net> <4f5bf4f9.1965115079@text.giganews.com> <9s2cmhFf52U1@mid.individual.net> <4f5c2af2.1978932789@text.giganews.com> <0.5b82e106999cbb8812e8.20120311115929GMT.87aa3n8i1q.fsf@bsb.me.uk>

Show all headers | View raw


On Sun, 11 Mar 2012 11:59:29 +0000, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>cri@tiac.net (Richard Harter) writes:
>
>> On Sun, 11 Mar 2012 14:15:52 +1300, Ian Collins <ian-news@hotmail.com>
>> wrote:
><snip>
>>>>> Why don't you just use the usual union in a struct technique?
>>>>
>>>> You might spell out what you think "the usual"  is; I looked at
>>>> several variations and it looked like there were subtle problems with
>>>> all of them.  Perhaps I missed something.
>>>
>>>struct load_package {
>>>   message_type type;       /* The type has access info    */
>>>   size_t       total_len;  /* Total length of the package */
>>>   size_t       data_len;   /* Length of the data          */
>>>   union {
>>>     payload_type1;
>>>     payload_type2;
>>>   } payload;
>>>};
>>
>> Unless you have something in mind that I'm missing this won't do at
>> all.  First of all there is no guarantee of alignment - there may have
>> to be padding between data_len and the union.  Secondly I really hope
>> you aren't suggesting that every different payload type in a program
>> must be in that union.
>
>There is another "usual" union method where the members are used only to
>ensure alignment and not to designate payloads:
>
>  struct load_package {
>    message_type type;       /* The type has access info    */
>    size_t       total_len;  /* Total length of the package */
>    size_t       data_len;   /* Length of the data          */
>    union {
>      long double dummy_1;
>      double dummy_2;
>      long long int dummy_3;
>      void *dummy_4;
>      struct dummy { int i; } dummy_5;
>      /* and so on... */
>    } aligned[];
>  } example;
>
>You never use any of the dummy members, their purpose is only to ensure
>that
>
>  (void *)example.aligned;
>
>is aligned for (almost) any type.  (Sometimes it is worth giving some of
>the members sane names because it gives you a way to get typed pointers
>to the payload without a cast.)  Obviously, you can hide the ugliness
>away in another header if you prefer.
>
>The main problems is obvious -- can you be sure that maximal alignment
>has been achieved?  I think that, for all practical purposes, you can,
>but it's not an intellectually gratifying solution.
>
>The empty []s are C99.  You may need to use [1]
>
>Note that I've not read your original post, so I'm replying just on this
>detailed point.  It's therefore possible that the above fails some other
>requirement, but it's easier for you to check that than for me go find
>(and read) your OP!


I've used that one in the past.  As you say, for all practical
purposes it works.  At least it does until a wave of architecture
changes arrives.  One of the nasty things about these "for all
practical purposes" solutions is that they can make for latent bugs
that don't show up until many years later.

What I am hoping for is a coding technique that is "bullet proof",
i.e., it is guaranteed to work if one has a conforming C compiler.


Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-10 19:26 +0000
  Re: What is the right way to do this simple thing Don Y <this@isnotme.com> - 2012-03-10 12:57 -0700
  Re: What is the right way to do this simple thing ImpalerCore <jadill33@gmail.com> - 2012-03-10 14:12 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-10 23:31 +0000
      Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-11 13:06 +1300
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 00:44 +0000
          Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-11 14:15 +1300
            Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 04:46 +0000
              Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-11 21:27 +1300
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 17:23 +0000
                Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-12 10:01 +1300
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:18 +0000
                Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-14 10:23 +1300
              Re: What is the right way to do this simple thing Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-11 11:59 +0000
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 16:51 +0000
                Re: What is the right way to do this simple thing Gene <gene.ressler@gmail.com> - 2012-03-13 07:32 -0700
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:46 +0000
      Re: What is the right way to do this simple thing ImpalerCore <jadill33@gmail.com> - 2012-03-12 07:28 -0700
        Re: What is the right way to do this simple thing ImpalerCore <jadill33@gmail.com> - 2012-03-12 08:13 -0700
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:51 +0000
  Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-10 14:21 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 01:40 +0000
      Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-11 14:11 -0700
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:56 +0000
          Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-14 14:55 -0700
            Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-15 21:55 +0000
              Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-19 13:33 -0700
  Re: What is the right way to do this simple thing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-10 15:21 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 00:47 +0000
  Re: What is the right way to do this simple thing Philip Lantz <prl@canterey.us> - 2012-03-11 01:07 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 16:14 +0000
  Re: What is the right way to do this simple thing Markus Wichmann <nullplan@gmx.net> - 2012-03-11 13:47 +0100
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 16:24 +0000
  Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 21:01 +0000
    Re: What is the right way to do this simple thing Anders Wegge Keller <wegge@wegge.dk> - 2012-03-12 00:05 +0100
      Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:57 +0000
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 23:06 +0000
      Re: What is the right way to do this simple thing Geoff <geoff@invalid.invalid> - 2012-03-11 17:34 -0700
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 22:00 +0000
          Re: What is the right way to do this simple thing Geoff <geoff@invalid.invalid> - 2012-03-13 18:12 -0700
            Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-14 03:38 +0000
              Re: What is the right way to do this simple thing Geoff <geoff@invalid.invalid> - 2012-03-14 00:49 -0700

csiph-web