2

Possible Duplicate:
How do I find the size of a struct?
Struct varies in memory size?

I am using following struct for network communication, It creates lots of unnecessary bytes in between.

It gives different size than expected 8 Bytes.

struct HttpPacket {
  unsigned char x1;
  union {
        struct {
           unsigned char  len;
           unsigned short host;
           unsigned char content[4];
        } packet;
        unsigned char bytes[7];
        unsigned long num;
    }

And Following gives different size even though that I am removing a field from a union

struct HttpPacket {
             unsigned char x1;
             union {
            struct {
               unsigned char  len;
               unsigned short host;
               unsigned char content[4];
            } packet;
            unsigned long num;
        }

Also, A more clear example

struct {
               unsigned char  len;
               unsigned short host;
               unsigned char content[4];
            } packet;

And it gives a size of 8, instead of 7. And I add one more field, It still gives the same size

struct {
               unsigned char  EXTRAADDEDFIELD;
               unsigned char  len;
               unsigned short host;
               unsigned char content[4];
            } packet;

Can someone please help on resolving this issue ?

UPDATE: I need the format to hold while transmitting the packet, So I want to skip these paddings

3
  • I need the format to hold while transmitting the packet, So I want to skip these paddings Dec 16, 2011 at 20:05
  • Need this on XCode 4 LLVM Compiler Dec 16, 2011 at 20:10
  • But, I need the solution for XCode 4 new LLVM Compiler. Dec 16, 2011 at 20:16

4 Answers 4

10

C makes no guarantees on the size of a struct. The compiler is allowed to line up the members however it wants. Usually, as in this case, it will make the size word-aligned since that's fastest on most machines.

3
  • 1
    Some compiles support pragmas to disable padding. This is most often seen in embeded development but I don't know if it is restricted to that.
    – Lou
    Dec 16, 2011 at 19:56
  • 1
    +1 for the first polite response.
    – Lou
    Dec 16, 2011 at 19:56
  • Some extra reading: en.wikipedia.org/wiki/Sizeof#Structure_padding
    – Jon
    Dec 16, 2011 at 19:58
7

Ever heard of alignment and padding?

Basically, to ensure fast access, certain types have to be on certain bounds of memory addresses.
This is called alignment.

To achieve that, the compiler is allowed to insert bytes into your data structure to achieve that alignment.
This is called padding.

2

By default, structure fields are aligned on natural boundaries. For example, a 4-byte field will start on a 4-byte boundary. The compiler inserts pad bytes to achieve this. You can avoid the padding by using #pragma pack(0) or other similar compiler directives

1
  • How to do that on LLVM XCode 4 compiler Dec 16, 2011 at 20:06
0

If you have a C99 compiler and can use the "new" fixed-width types: make an array of uint8_t and do the separation in members yourself.

uint8_t data[8];

x1 = data[0];
len = data[1];
host = data[2] * 256 + data[3]; /* big endian */
content[0] = data[4];
content[1] = data[5];
content[2] = data[6];
content[3] = data[7];

/* ... */

You can follow the same procedure in C89 if you can rely on CHAR_BIT being 8.

Not the answer you're looking for? Browse other questions tagged or ask your own question.