![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
Help With Struct Size Issue
Code below defines WORD and DWORD types, and a struct BITMAPFILEHEADER that uses only those types. WORD should be 2 bytes, DWORD 4.
If I do a sizeof(BITMAPFILEHEADER), output is ALWAYS 16 if I use a DWORD for 1 or 2 members in the struct. It should be 10 for 1 member with DWORD, and 14 for 2 members. If I use WORD for all 5 members, sizeof = 10, which is correct. But I always get 16 if 1 or 2 members are DWORD. Anyone have any idea why this would happen? I've been staring at it for hours and can't seem to see the problem. I have 4 other structs that are all the correct sizes, and they use WORD and DWORD, as well. typedef unsigned short WORD;
typedef unsigned long DWORD;
// see "BITMAPFILEHEADER" in Platform SDK docs
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER; This is driving me crazy!
__________________
Neeley.org |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
It is probably because of structure packing. The compiler will try to put your DWORDS on 4 byte boundaries, so that they are faster for the CPU to process.
How you fix it depends on what compiler you are using. Under visual C you can use #pragma pack (1) ... define structures #pragma pack () Which will pack everything as tightly as possible. The second pragma sets the value back to the default. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
Thank you, The Dark. You rock.
__________________
Neeley.org |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|