Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2006, 7:21 PM   #1
dr.p
Programmer
 
dr.p's Avatar
 
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3 dr.p is on a distinguished road
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;
Thanks for looking This is driving me crazy!
__________________
Neeley.org
dr.p is offline   Reply With Quote
Old Apr 20th, 2006, 8:08 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Apr 20th, 2006, 8:14 PM   #3
dr.p
Programmer
 
dr.p's Avatar
 
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3 dr.p is on a distinguished road
Thank you, The Dark. You rock.
__________________
Neeley.org
dr.p is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC