|
-
Jan 19th, 2003, 08:27 PM
#1
Thread Starter
Addicted Member
sizeof( ) doesn't add up!
Hi,
I noticed something quirky with getting the size of a structure and comparing that to the sum of its individual parts. The size of the structure is 16 but the sum of the parts total 14. Any explanations? :-)
Regards,
ChuckB
Code:
typedef struct {
unsigned short type; //WORD
unsigned long size; //DWORD
unsigned short reserved1; //WORD
unsigned short reserved2; //WORD
unsigned long offset; //DWORD
} HEADER;
HEADER h;
cout <<"type: " << sizeof(h.type) << endl; //2 bytes
cout <<"size: " << sizeof(h.size) << endl; //4 bytes
cout <<"reserved1: " << sizeof(h.reserved1) << endl; //2 bytes
cout <<"reserved2: " << sizeof(h.reserved2) << endl; //2 bytes
cout <<"offset: " << sizeof(h.offset) << endl; //4 bytes
cout <<"---------------------" << endl;
cout <<"Size of h: " << sizeof(h) << endl << endl;; //16 bytes
cout <<"Size of HEADER: " << sizeof(HEADER) << endl; //16 bytes
-
Jan 20th, 2003, 12:45 AM
#2
Hyperactive Member
It seems that parksie was correct here.
http://www.vbforums.com/showthread.p...TMAPFILEHEADER
Look at the 4th post from the last.
http://www.codeguru.com/forum/showth...ighlight=ALIGN
Use #pragma pack to make your own structure 16 bytes, just like Windows' equivalent.
Hope it helps.
-
Jan 20th, 2003, 02:10 AM
#3
Thread Starter
Addicted Member
Hi,
Thanks for the tip on 'pragma pack'. If you noticed your first link, I started that thread, but got lost listening to the 'pros' talk. I'll experiment with this boundary stuff.
Thanks,
ChuckB
-
Jan 20th, 2003, 03:20 AM
#4
Hyperactive Member
Maybe you would like to look up the declaration of tagBITMAPFILEHEADER in wingdi.h, and pshpack2.h and poppack.h . Something is there!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|