编译器的地址对齐问题

我们看 CanRxMsg


typedef struct
{
  uint32_t StdId;  /* Specifies the standard identifier.
                      This parameter can be a value between 0 to 0x7FF. */

  uint32_t ExtId;  /* Specifies the extended identifier.
                      This parameter can be a value between 0 to 0x1FFFFFFF. */

  uint8_t IDE;     /* Specifies the type of identifier for the message that
                      will be received. This parameter can be a value of
                      @ref CAN_identifier_type */

  uint8_t RTR;     /* Specifies the type of frame for the received message.
                      This parameter can be a value of
                      @ref CAN_remote_transmission_request */

  uint8_t DLC;     /* Specifies the length of the frame that will be received.
                      This parameter can be a value between 0 to 8 */

  uint8_t Data[8]; /* Contains the data to be received. It ranges from 0 to
                      0xFF. */

  uint8_t FMI;     /* Specifies the index of the filter the message stored in
                      the mailbox passes through. This parameter can be a
                      value between 0 to 0xFF */
} CanRxMsg;


Data字段作为一个 uint8_t 数组,起始地址并未对齐到 DWORD,而是紧跟着 DLC?

您好,数组类型为uint_8,则按照该类型大小进行内存对齐,并不是将该数组作为整体计算大小进行对齐。
您可使用__attribute__((aligned(4))) uint8_t Data[8];控制该成员变量的对齐形式,从而达到数组四字节对齐的效果。


只有登录才能回复,可以选择微信账号登录