结构体内存对齐问题
想自己写个读取BMP文件的类
24位BMP图 头文件是54字节
写了如下结构体
typedef struct BitmapHeader
{
char id[2]; // 这里补了2字节
unsigned int file_size;
unsigned int reserved;
unsigned int bitmap_data_offset;
unsigned int bitmap_header_size;
unsigned int width;
unsigned int height;
unsigned short pianes;
unsigned short bit_depth;
int compression;
unsigned int bitmap_data_size;
unsigned int hres;
unsigned int vres;
unsigned int colors;
unsigned int important_colors;
}BitmapHeader;
结构体对齐的问题 导致这个结构实际占用56字节
把文件里前54字节复制到这个结构变量就会出现数据不正常
一般这种情况 用什么方法解决比较好
[解决办法]
#pragma pack(1)