Reputation: 6044
I'm trying to use struct to map header of a BitMap file. It seems that compiler is doing 4byte (32bit) alignment but I need 2Byte. I tried to change that via complier directive as below
#pragma pack(2)
and
__attribute__ ((aligned(xx)));
those two doesn't seem to have any effect. Is there another way to do this? I'm using XCode 4.3 on Mac OS X Lion. I Tested both Apple LLVM and Apple GCC compliers.
Here is the Struct type definition
typedef struct {
int16_t bfType;
int32_t bfSize;
int16_t bfReserved1;
int16_t bfReserved2;
int32_t bfOffBits;
int32_t biSize;
int32_t biWidth;
int32_t biHeight;
int16_t biPlanes;
int16_t biBitCount;
int32_t biComression;
int32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
int32_t biClrUsed;
int32_t biClrImportant;
} THeader;
Upvotes: 1
Views: 2161
Reputation: 94584
Huh? works on my machine? Bear in mind that the pack pragma is possibly being overridden somewhere else?
#include <inttypes.h>
#include <stddef.h>
#pragma pack(push,2)
typedef struct {
int16_t bfType;
int32_t bfSize;
int16_t bfReserved1;
int16_t bfReserved2;
int32_t bfOffBits;
int32_t biSize;
int32_t biWidth;
int32_t biHeight;
int16_t biPlanes;
int16_t biBitCount;
int32_t biComression;
int32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
int32_t biClrUsed;
int32_t biClrImportant;
} THeader;
#pragma pack(pop)
#include <stdio.h>
int main(void)
{
printf("%lu\n", offsetof(THeader, bfType));
printf("%lu\n", offsetof(THeader, bfSize));
printf("%lu\n", offsetof(THeader, bfReserved1));
printf("%lu\n", offsetof(THeader, bfReserved2));
return 0;
}
$ clang -o pack pack.c
$ ./pack
0
2
6
8
Upvotes: 2