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

Confused by keil mdk struct alignment

typedef uint8_t BDADDR_t[6];
typedef struct
{
        uint8_t  State;
        BDADDR_t RemoteBDADDR;
        uint16_t DataPacketsQueued;
        uint16_t Handle;
        uint8_t  LinkType;
        uint8_t  CurrentIdentifier;
        bool     LocallyInitiated;
}ATTR_PACKED BT_HCI_Connection_t;
BT_HCI_Connection_t HCIConnection;

The address of HCIConnection is:0x1000004E and the memory data start from 0x1000004E is as following:
03 10 a3 15 83 15 00 00.
So I think the State = 03; the BDADDR is 00 15 83 15 a3 10, but the watch window show that the BDADDR is 00 00 15 83 15 a3.
So what should I do to get the correct value of a mix typed struct?

Another question:
Does the effect of #pragma pack(1) is the same as __packed ?
Thanks!

chris

Parents
  • Exactly - in this case the struct have the size and location of all fields carefully selected and ordered to maximize the probability of compilers placing them similarly. Two byte variables can be stored before or after a 16-bit variable and two 16-bit variables can be stored before/after a 32-bit variable while maintaining 16-bit or 32-bit align without need for any pad.

    It is always possible to place struct fields in ways that gives better/worse probability. Then the developer have to decide the value of _assuming_ that no extra padding have been added contra the oops if the assumption was wrong.

    Next thing is that networking code contains macros or functions for converting fields between network and host byte order.

Reply
  • Exactly - in this case the struct have the size and location of all fields carefully selected and ordered to maximize the probability of compilers placing them similarly. Two byte variables can be stored before or after a 16-bit variable and two 16-bit variables can be stored before/after a 32-bit variable while maintaining 16-bit or 32-bit align without need for any pad.

    It is always possible to place struct fields in ways that gives better/worse probability. Then the developer have to decide the value of _assuming_ that no extra padding have been added contra the oops if the assumption was wrong.

    Next thing is that networking code contains macros or functions for converting fields between network and host byte order.

Children
No data