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


Groups > comp.lang.c > #18489

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

From Tim Rentsch <txr@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: What is the right way to do this simple thing
Date 2012-03-10 14:21 -0800
Organization A noiseless patient Spider
Message-ID <kfnlin8nllo.fsf@x-alumni2.alumni.caltech.edu> (permalink)
References <4f5ba2ed.1944110152@text.giganews.com>

Show all headers | View raw


cri@tiac.net (Richard Harter) writes:

> Suppose that I have some data that is pointer free.  It isn't
> necessarily serialized in some format such as JSON but I would like to
> be able to copy it from one from one machine to another compatible
> machine and use it without trouble.  Also I don't want to pass around
> a collection of items and I want everything to be properly aligned.
>
> Naturally I think of using the struct hack and a structure something
> like this:
>
>    struct 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          */
>        char[1]      data;       /* The actual data             */
>    };
>
> Presumably at some point I will copy said data into an instance of the
> package which can then be passed around.  Also I really would like to
> be able to use the data upon receipt without copying it out, e.g.,
>
>    local_data = (data_type *)(pkg->data);
>
> It occurs to me that this might not work there is no guarantee that
> pkg->data is correctly aligned.  There are a whole bunch of other
> potential gotchas that I can foresee.  This is particularly troubling
> to me because my PC has four cores, each with a different DS9000 chip.
> What's worse my boss is on a "compiler of the week" binge.  Every week
> we get to test a new compiler.  I really want to get this code
> bullet-proof.
>
> Help!  What is the right way to do this simple thing?

I was in the middle of a somewhat longer reply when I changed
my mind and wrote this instead.

It's hard to give an answer without more information.  Some
of the key questions are:

  1. are the package struct's being passed through shared
     memory or through some sort of serial interface (such
     as file writing/reading or a network socket)?

  2. is it guaranteed that the raw data bytes will work on
     all the different machines used provided the base is
     suitably aligned?

  3. is 'message_type' a scalar type, or is it something
     more complicated that could be affected by changing
     between processors or compilers?

  4. I assume that all the different machines used will
     work identically on integer data types as long as
     the types have the same length and the data values
     have the same alignment from one machine to the next.
     Is this assumption correct, or might it be incorrect?
     If incorrect, then please say more.

  5. if shared memory is going to be used - is malloc()
     guaranteed to return a memory block that is suitably
     aligned in, and for, all the different execution
     environments used?  If not, please say more.

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