This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Packed Structs with Bitfields

Hi Guys,

I'm programing my own TCP/Ip Stack. Therefore I need structs with bitfields.

For eg:

typedef struct  __attribute__ ((packed))
{
        uint16_t srcPort;
        uint16_t dstPort;
        uint32_t seq;
        uint32_t ack;
        unsigned headerlen      : 4;
        unsigned res            : 6;
        unsigned flags          : 6;
        uint16_t hlenFlags;
        uint16_t windowSize;
        uint16_t checksum;
        uint16_t urgentPtr;
}
TCP_Header

After the bitfield variables the values aren't right anymore. Can you tell me why is this so?

Thanks for your help
best regards
Robert

Parents
  • The only thing you can do is change the order of the bitfields in the declaration. But the compiler will not (!) allow you full control of bit fields. A specific compiler will always follow the same rules (until possibly the rules changes when you update to the next version).

    In the case of ARM compilers, ARM has specified an ABI that locks down what the compiler should do.

Reply
  • The only thing you can do is change the order of the bitfields in the declaration. But the compiler will not (!) allow you full control of bit fields. A specific compiler will always follow the same rules (until possibly the rules changes when you update to the next version).

    In the case of ARM compilers, ARM has specified an ABI that locks down what the compiler should do.

Children