首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

结构体内存对齐有关问题

2013-01-19 
结构体内存对齐问题想自己写个读取BMP文件的类24位BMP图 头文件是54字节写了如下结构体typedef struct Bit

结构体内存对齐问题
想自己写个读取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)

热点排行