Results 1 to 4 of 4

Thread: sizeof( ) doesn't add up!

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    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
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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.

  3. #3

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    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
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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
  •  



Click Here to Expand Forum to Full Width