Question on using different types within a struct.

Discussion in 'C Programming' started by Durango, Mar 8, 2012.

  1. Durango

    Durango Guest

    I am looking to use structs in my program so that I can pass them as
    "packets" over a network socket created within the program.

    I am particularly interested in the following scenario:

    typedef struct A
    {
    int a;
    int b;
    int c;
    }pkt_A;

    typedef struct B
    {
    int def;
    char msg[32];
    }pkt_B;

    typedef struct C
    {
    double d;
    char msg1[32];
    char msg2[32];
    }pkt_C;

    typedef struct D
    {
    int code;
    void *body;
    }pkt_D;


    Now I want struct D to act as the container for the other structs that
    are created. I am wondering if this would work or if there is a better
    way to do this? I am not sure if I am on the right track.

    Thanks for any advice.
     
    Durango, Mar 8, 2012
    #1
    1. Advertising

  2. Durango <> writes:

    > Now I want struct D to act as the container for the other structs
    > that are created. I am wondering if this would work or if there is
    > a better way to do this? I am not sure if I am on the right track.


    typedef struct D {
    int code;
    union {
    pkt_A a;
    pkt_B b;
    pkt_C c;
    }
    }

    This will work as long as code identifies which of the three data
    structures are used.

    --
    /Wegge

    Leder efter redundant peering af dk.*,linux.debian.*
     
    Anders Wegge Keller, Mar 8, 2012
    #2
    1. Advertising

  3. Durango <> wrote:
    > I am looking to use structs in my program so that I can pass them as
    > "packets" over a network socket created within the program.


    First of all sending structures over the net is typically
    not a good idea because a structure created on machine A,
    using the exact same code, can look rather different from
    one created on machine B. Even somethig simple as an int
    can have different sizes on different machines, the binary
    representation of the values may be different (i.e. you
    may have different endian-ness on the machines). Then
    the compiler may insert different amounts of "padding"
    between the members of the structure - even if the archi-
    tectures of machine A and B are identical, i.e. use the
    same sizes and endian-ness for all types you use the
    same structure created by different compiler or even
    different versions of the same compiler can be different.

    So sending structures from one machine to another (or
    just storing data by writing a structure directly to
    a file) is usually a rather bad idea (I tried that my-
    self once before I understood the implictions and got
    bitten by it badly;-)

    The simplest way to transmit (or store) values is
    by converting them to ASCII strings. Simple, if the
    data are already strings, also very simple for inte-
    ger types, a bit more tricky for floating point
    values since you have to consider which precision
    you need.

    > I am particularly interested in the following scenario:


    > typedef struct A
    > {
    > int a;
    > int b;
    > int c;
    > }pkt_A;


    > typedef struct B
    > {
    > int def;
    > char msg[32];
    > }pkt_B;


    > typedef struct C
    > {
    > double d;
    > char msg1[32];
    > char msg2[32];
    > }pkt_C;


    > typedef struct D
    > {
    > int code;
    > void *body;
    > }pkt_D;


    This looks completely wrong - a pointer on the machine you
    send this structure from has no meaning on the machine you
    receive it on, so it can't be used for anything.

    > Now I want struct D to act as the container for the other structs that
    > are created. I am wondering if this would work or if there is a better
    > way to do this? I am not sure if I am on the right track.


    What do you mean by "as the container"? You have four struc-
    tures of different sizes, how could be one of them with pro-
    bably one of the smallest sizes be used as a "container" for
    the others?

    Did you intent to set the 'code' member of 'pkt_D' to some-
    thing indicate the type of one of the other three and make
    the 'body' member point to it and then send just this over
    the network? If that's the case that won't work at all.
    When the other machine gets the structure of type 'pkt_D'
    (even when assuming that both machines are identical and
    you used the same compiler and version on both of them
    to compile the program) having a pointer on B that is
    the address of something in the memory of machine A is
    rather useless since the program running on B can't access
    that (even if these would be two programs running on the
    same machine it wouldn't work since typically, i.e. on
    machines with a virtual memory system, a program can't
    access the memory of another).

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 8, 2012
    #3
  4. Durango

    Durango Guest

    On Thu, 08 Mar 2012 10:28:09 +0000, Jens Thoms Toerring wrote:

    > Durango <> wrote:
    >
    > Regards, Jens


    Thank you for your input Jens, it makes sense now after I read your post.
    After some more research I will look into using serialization method to
    accomplish this.
     
    Durango, Mar 9, 2012
    #4
    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. RA Scheltema
    Replies:
    3
    Views:
    410
    RA Scheltema
    Jan 6, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  3. Michael B Allen

    Tail space in struct within struct

    Michael B Allen, Oct 23, 2004, in forum: C Programming
    Replies:
    3
    Views:
    305
    Michael B Allen
    Oct 23, 2004
  4. __PaTeR
    Replies:
    7
    Views:
    489
    Barry Schwarz
    Jan 1, 2009
  5. John Reye
    Replies:
    28
    Views:
    1,388
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page