1.下面这段代码取消了结构体字节对齐优化,运行的结果为5个字节。
#include <stdio.h>
#include <stdlib.h>
#pragma pack(1)
typedef struct IntPair_s {
char c;
int num;
} IntPair;
#pragma pack()
int main(void) {
size_t IntPairSize = sizeof( IntPair );
printf( "Size of IntPair = %d\n", IntPairSize);
return 0;
}
2.注释掉#pragma pack(1)和#pragma pack(),运行结果为8个字节。
#include <stdio.h>
#include <stdlib.h>
//#pragma pack(1)
typedef struct IntPair_s {
char c;
int num;
} IntPair;
//#pragma pack()
int main(void) {
size_t IntPairSize = sizeof( IntPair );
printf( "Size of IntPair = %d\n", IntPairSize);
return 0;
}
关注公众号《首飞》回复“机器人”获取精心推荐的C/C++,Python,Docker,Qt,ROS1/2,机器人学等机器人行业常用技术资料。