Unions and structures implementation in C

Discussion in 'C Programming' started by Jason Curl, Oct 12, 2005.

  1. Jason Curl

    Jason Curl Guest

    Hello,

    Does the C standard specify (and if so, how, or possibly a reference to
    the standard - I don't have a copy). I am also assuming that CHAR_BIT is
    8 (for Posix systems)

    a) How elements in a union overlap
    b) limitations on how elements in a structure are padded

    This will help me in writing (hopefully) portable C code that can
    communicate across different architectures, that may be faster and more
    memory efficient than copying from one buffer to another.

    Or is the only practical way to memcpy() taking into account endianness
    of the machine into a "char *buffer"?

    Thanks,
    Jason.
    Jason Curl, Oct 12, 2005
    #1
    1. Advertising

  2. Jason Curl

    pemo Guest

    "Jason Curl" <> wrote in message
    news:diidc8$4ru$...
    > Hello,
    >
    > Does the C standard specify (and if so, how, or possibly a reference to
    > the standard - I don't have a copy). I am also assuming that CHAR_BIT is 8
    > (for Posix systems)
    >
    > a) How elements in a union overlap
    > b) limitations on how elements in a structure are padded
    >
    > This will help me in writing (hopefully) portable C code that can
    > communicate across different architectures, that may be faster and more
    > memory efficient than copying from one buffer to another.
    >
    > Or is the only practical way to memcpy() taking into account endianness of
    > the machine into a "char *buffer"?


    You can get a copy of the c99 std from the ANSI website for $18 - might be
    worth investing?
    pemo, Oct 12, 2005
    #2
    1. Advertising

  3. Jason Curl

    Eric Sosman Guest

    Jason Curl wrote:
    > Hello,
    >
    > Does the C standard specify (and if so, how, or possibly a reference to
    > the standard - I don't have a copy). I am also assuming that CHAR_BIT is
    > 8 (for Posix systems)
    >
    > a) How elements in a union overlap


    All the union's elements start at the same address.
    More formally, a pointer to the union can be converted
    to a pointer to any of its elements, and vice versa.

    > b) limitations on how elements in a structure are padded


    Padding may appear after any element of the struct.
    (People often say "Between any elements, and at the end
    of the struct," but that's just a longer way of saying
    the same thing.) The struct's first element starts at
    the struct's address; more formally, a pointer to the
    struct can be converted to a pointer to its first element,
    and vice versa.

    > This will help me in writing (hopefully) portable C code that can
    > communicate across different architectures, that may be faster and more
    > memory efficient than copying from one buffer to another.
    >
    > Or is the only practical way to memcpy() taking into account endianness
    > of the machine into a "char *buffer"?


    memcpy() might not be enough. For example, you may
    want to communicate a `long' value, but `long' on machine A
    is 32 bits while on machine B it's 64. Or perhaps you want
    to move a `long double' value between a machine were it is
    actually more precise than plain `double' and a machine
    where it's really just a plain `double' with a different name.
    Mere memcpy() cannot reconcile such differences.

    What's usually needed is a careful specification of the
    external format -- the way the information looks "on the
    wire" or "in the file." Then for each machine you write a
    pair of functions (or a set of pairs): one to convert the
    machine's idiosyncratic internal data representation to the
    external form, and one to digest the external form and convert
    it to the internal representation. A visit to someplace like
    wotsit.org may give you some ideas about how to specify an
    external format that has a reasonable chance at portability:
    study the way JFIF or WAV files (for example) are described,
    and see if you can steal, er, "be inspired by" their techniques.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 12, 2005
    #3
  4. Jason Curl a écrit :
    > Does the C standard specify (and if so, how, or possibly a reference to
    > the standard - I don't have a copy). I am also assuming that CHAR_BIT is
    > 8 (for Posix systems)
    >
    > a) How elements in a union overlap
    > b) limitations on how elements in a structure are padded


    Are your facing such questions probably means that you have a design
    error. Unions are used to save memory, but certainely not to do parallel
    memory mappings.

    Arrays of unsigned char and bitwise operators are your friends.
    Emmanuel Delahaye, Oct 12, 2005
    #4
  5. Jason Curl

    Default User Guest

    Jason Curl wrote:

    > Hello,
    >
    > Does the C standard specify (and if so, how, or possibly a reference
    > to the standard - I don't have a copy). I am also assuming that
    > CHAR_BIT is 8 (for Posix systems)
    >
    > a) How elements in a union overlap
    > b) limitations on how elements in a structure are padded
    >
    > This will help me in writing (hopefully) portable C code that can
    > communicate across different architectures, that may be faster and
    > more memory efficient than copying from one buffer to another.
    >
    > Or is the only practical way to memcpy() taking into account
    > endianness of the machine into a "char *buffer"?



    There's no way you're going to achieve what you want with unions and
    memcpy and such. Cross-platform communication has many potential traps
    and pitfalls. Besides structure padding, there are issues with size and
    endianess of basic data types.

    Look up the topic of serialization. You will find much prior art on the
    subject. The FAQ from another newsgroup, comp.lang.c++ has some
    information.


    http://www.parashift.com/c -faq-lite/serialization.html#faq-36.1



    Brian
    Default User, Oct 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    9
    Views:
    409
    Jeffrey D. Smith
    Oct 16, 2003
  2. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
  3. Replies:
    20
    Views:
    686
    Richard
    Aug 10, 2007
  4. Ravikiran

    Difference between Unions and Structures...

    Ravikiran, Nov 3, 2008, in forum: C Programming
    Replies:
    7
    Views:
    604
    CBFalconer
    Nov 3, 2008
  5. Ravikiran

    Difference between Unions and Structures...

    Ravikiran, Nov 3, 2008, in forum: C Programming
    Replies:
    2
    Views:
    306
    MJ_India
    Nov 3, 2008
Loading...

Share This Page