记一次结构体字节对齐引起的严重问题

头文件自定义了bool类型,4个字节:

#ifndef bool
#define bool int
#define true 1
#define false 0
#endif

typedef struct {
   bool automaticActivation;
   bool testCall;
   bool positionCanBeTrusted;
   msd_VehicleType_t vehType;
} msd_Control_t;

typedef struct {
   uint8_t messageIdentifier;
   msd_Control_t  control;
   char vehIdentificationNumber[18];
   msd_VehiclePropulsionStorageType_t  vehPropulsionStorageType;
} msd_Structure_t;

而另一个文件因为包含了标准头文件,bool对应的一个字节:

#include <stdbool.h>

int main()
{
    msd_t msd = {0};
    uint8_t builtMsd[QL_ECALL_MSD_MAX_LEN];    
    g_debug("VIN: %17s",(char*)msd.msdMsg.msdStruct.vehIdentificationNumber0;
    encodeMsdMessage(&msd, builtMsd);
}

main()中打印的VIN:ECALLEXAMPLE02013
encodeMsdMessage()中打印的VIN:MPLE02013, 少了8个字符.

原因就是main()编译使用的一个字节的bool,encodeMsdMessage()编译用的是自定义的4字节bool.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值