padding struct

Discussion in 'C Programming' started by luvraghu@gmail.com, Jul 27, 2012.

  1. Guest

    Hello Experts,
    suppose i have a packet of 9 bytes having fields: length(4bytes), type(1 byte), version(4bytes). Now i would write the packet structure as
    struct mypack {
    unsigned int;
    unsigned char;
    unsigned int;
    };
    when i allocate the structure as:
    ptr = (struct mypack *)malloc(sizeof(struct mypack));

    the amount of memory allocated is 12bytes(3 bytes extra due to structure padding). If this packet is sent out,how would the receiver receive the correct packet contents, I mean he would know the offset at which each field is present and when he tries to get those field value at that offset he might get it wrong.. for example if he tries to get the 3rd field version which is at offset 6 he would get the wrong value due to the structure padding after char member in struct. how can we solve this? I understand pragma directives avoid padding, but what if I dont want to use them?

    Hope I'm clear. Please clarify.

    Thanks for your time.
     
    , Jul 27, 2012
    #1
    1. Advertising

  2. Mark Bluemel Guest

    On 27/07/2012 10:52, wrote:
    > Hello Experts,
    > suppose i have a packet of 9 bytes having fields: length(4bytes), type(1 byte), version(4bytes). Now i would write the packet structure as
    > struct mypack {
    > unsigned int;
    > unsigned char;
    > unsigned int;
    > };
    > when i allocate the structure as:
    > ptr = (struct mypack *)malloc(sizeof(struct mypack));
    >
    > the amount of memory allocated is 12bytes(3 bytes extra due to structure padding). If this packet is sent out,how would the receiver receive the correct packet contents, I mean he would know the offset at which each field is present and when he tries to get those field value at that offset he might get it wrong.. for example if he tries to get the 3rd field version which is at offset 6 he would get the wrong value due to the structure padding after char member in struct. how can we solve this? I understand pragma directives avoid padding, but what if I dont want to use them?
    >
    > Hope I'm clear. Please clarify.
    >
    > Thanks for your time.
    >

    I think you should read section 2 of the C FAQ.

    Questions 2.11 and 2.12 (including the supplementary information) would
    probably help you.

    You'll find the FAQ at http://c-faq.com/
     
    Mark Bluemel, Jul 27, 2012
    #2
    1. Advertising

  3. On Friday, July 27, 2012 4:52:27 AM UTC-5, wrote:
    > Hello Experts,
    >
    > suppose i have a packet of 9 bytes having fields: length(4bytes), type(1 byte), version(4bytes). Now i would write the packet structure as
    >
    > struct mypack {
    >
    > unsigned int;
    >
    > unsigned char;
    >
    > unsigned int;
    >
    > };
    >
    > when i allocate the structure as:
    >
    > ptr = (struct mypack *)malloc(sizeof(struct mypack));
    >
    >
    >
    > the amount of memory allocated is 12bytes(3 bytes extra due to structure padding). If this packet is sent out,how would the receiver receive the correct packet contents, I mean he would know the offset at which each field is present and when he tries to get those field value at that offset he might get it wrong.. for example if he tries to get the 3rd field version whichis at offset 6 he would get the wrong value due to the structure padding after char member in struct. how can we solve this? I understand pragma directives avoid padding, but what if I dont want to use them?
    >
    >
    >
    > Hope I'm clear. Please clarify.
    >
    >
    >
    > Thanks for your time.


    There is no portable way to eliminate the padding. Your compiler may provide a system specific method of doing so. But does the recipient's?

    Furthermore, you have no idea if the padding generated by your compiler will match the padding generated by the recipient's compiler.

    You also don't know if the sizeof(int) on your system is the same as the recipient's. The same is true for endianness.

    That being said, in my experience most compilers will not generate padding if each of the members is a char or an array of char. Once you construct the values for each member is a set of working variables, you can use a combination of shift operators and bitwise-and operators to construct the members in a documented format. The recipient can use a similar approach to reconstructing the values.
     
    Barry Schwarz, Jul 27, 2012
    #3
  4. Jorgen Grahn Guest

    On Fri, 2012-07-27, wrote:
    > Hello Experts,


    > suppose i have a packet of 9 bytes having fields: length(4bytes),
    > type(1 byte), version(4bytes). Now i would write the packet structure
    > as


    > struct mypack {
    > unsigned int;
    > unsigned char;
    > unsigned int;
    > };


    My standard answer: don't use structs for I/O. Instead define the I/O
    data format in terms of octets (e.g. if this is something UDP-based)
    and write functions for filling in/reading from an uint8_t buffer
    according to that format.

    You can still have a struct, but it won't neccessarily match,
    bit-by-bit, the data in your I/O.

    Doing the struct-mapping thing IME leads to all kinds of problems:
    compiler dependencies, endianness bugs, buffer overflows, weakly
    defined data formats, lots of casting, valgrind warnings ...

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jul 28, 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. Chris Fogelklou
    Replies:
    36
    Views:
    1,439
    Chris Fogelklou
    Apr 20, 2004
  2. edware

    struct padding

    edware, Apr 25, 2006, in forum: C Programming
    Replies:
    9
    Views:
    451
    Ed Vogel
    Apr 27, 2006
  3. Hallvard B Furuseth

    Padding bits and struct assignment

    Hallvard B Furuseth, Dec 27, 2006, in forum: C Programming
    Replies:
    5
    Views:
    689
    Eric Sosman
    Dec 27, 2006
  4. Replies:
    11
    Views:
    896
  5. Replies:
    13
    Views:
    381
    glen herrmannsfeldt
    May 5, 2013
Loading...

Share This Page