size of structure
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: size of structure

  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    size of structure

    Can anyone tell why the size of this data structure is reported as 16 bytes when it should be 14 bytes ,

    typedef struct _Fheader
    {
    unsigned short a; 2 byes
    unsigned int imagesizeb; 4 bytes
    unsigned short resc; 2 bytes
    unsigned short resd; 4 bytes
    unsigned int offset; 2 bytes all = 14 byes

    } Fheader;


    int _tmain(int argc, _TCHAR* argv[])
    {
    Fheader fheader;
    cout << sizeof(Fheader) << endl ;


    Sleep(5000);

    return 0;

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: size of structure

    Quote Originally Posted by aamir121a View Post
    Can anyone tell why the size of this data structure is reported as 16 bytes when it should be 14 bytes ,
    This is dependent on the compiler and compiler settings you are using. It could be that in your case, a struct is always sized to a multiple of 4 bytes.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 1999
    Posts
    27,423

    Re: size of structure

    Quote Originally Posted by aamir121a View Post
    Can anyone tell why the size of this data structure is reported as 16 bytes when it should be 14 bytes ,
    First, this question has been asked so many times, if you did a search, you would find your answer.

    Second, there is a FAQ on this:
    http://www.codeguru.com/forum/showthread.php?t=276622

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: size of structure

    Adding #pragma pack(1) to the top of header files will cause it to be 14 bytes on some compilers, but not all.

    Compilers usually compile for optimum speed. You may also get 14 bytes if you use the Os compiling option, but I'm not sure about that.

  5. #5
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: size of structure

    What compiler are you using? On most 32-bit machines (including PCs), unsigned int == unsigned long.

    If you go back in time to the 80s, then unsigned int == unsigned short.

    You can never be sure what the size of an "int" is. Always use char (8 bit), short (16 bit), long (32 bit), and long long (64 bit). There are some compilers that don't properly support long long either.

    -Erik

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: size of structure

    Actually, that's incorrect. On most compilers long = long long, not int.

    Code:
    int main(){
        cout << sizeof(char) << endl;
        cout << sizeof(short) << endl;
        cout << sizeof(int) << endl;
        cout << sizeof(long) << endl;
        cout << sizeof(long long) << endl;
        return 0;
    }
    Code:
    1
    2
    4
    8
    8

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,247

    Re: size of structure

    Quote Originally Posted by egawtry
    On most 32-bit machines (including PCs), unsigned int == unsigned long.
    Quote Originally Posted by ninja9578
    Actually, that's incorrect. On most compilers long = long long, not int.
    Heheh, I think that this just confirms that these things are implementation defined, within the requirements set out by the C and C++ standards. Yet, egawtry did qualify with "most 32-bit machines (including PCs)", so he is not incorrect (other than the assumption that long will be 32-bit instead of 64-bit).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: size of structure

    thank you guys, however this interesting thing is if i change imagesizeb to short then sizeof structure drops to normal 12 bytes , so while i am changing imagesizeb to short from int the size drops to 12 bytes which i find inconsistent . from int to short the drop is 2 byes and the structure drops 4 byres.

    typedef struct _Fheader
    {
    unsigned short a; 2 byes
    unsigned int imagesizeb; 4 bytes
    unsigned short resc; 2 bytes
    unsigned short resd; 4 bytes
    unsigned int offset; 2 bytes all = 14 byes

    } Fheader;


    int _tmain(int argc, _TCHAR* argv[])
    {
    Fheader fheader;
    cout << sizeof(Fheader) << endl ;


    Sleep(5000);

    return 0;
    Reply With Quote

  9. #9
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: size of structure

    I tried this on windows 7 32 bit and 64 bit gcc mingw and vc 2008 / 2010 compliler resuls are the same

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,247

    Re: size of structure

    Quote Originally Posted by aamir121a
    however this interesting thing is if i change imagesizeb to short then sizeof structure drops to normal 12 bytes , so while i am changing imagesizeb to short from int the size drops to 12 bytes which i find inconsistent . from int to short the drop is 2 byes and the structure drops 4 byres.
    Here is one possibility: by default, your compilers are making it such there is alignment along a 4 byte boundary, with reasons due to computer architecture and efficiency. So, a is an unsigned short, and imagesizeb is an unsigned int. Assuming that the unsigned short is 2 bytes in size and the unsigned int is 4 bytes in size, the compiler decided not to place the unsigned int immediately after the unsigned short, since that would place it right smack in the middle of a 4 byte boundary. Therefore, it adds 2 bytes of padding after the unsigned short, leading to a total size of 16 bytes. If you change imagesizeb to be an unsigned short, it would no longer cross over a four byte boundary, thus the compiler does not add padding and the total size is 12 bytes.

    As Paul McKenzie noted, you could have read the FAQ and deduced this answer for yourself, or perhaps searched the Web for it (terms such as "structure alignment" and "byte padding" could come in handy).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: size of structure

    Thank you laser light and Paul McKenzie the answer is that padding is applied to structure , and doing pragma pack(1) will fix it to 14 bytes. thanks to ninja9578. do you know similar compiler flags for GCC / MINGW32 , Further how important is structure alignment when using to read information to structure from binary file header.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,423

    Re: size of structure

    Quote Originally Posted by aamir121a View Post
    Thank you laser light and Paul McKenzie the answer is that padding is applied to structure , and doing pragma pack(1) will fix it to 14 bytes. thanks to ninja9578. do you know similar compiler flags for GCC / MINGW32
    Most modern C++ compilers have #pragma's that set the structure alignment. Consult your compiler's documentation or search the web for the particular options.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Jan 2009
    Posts
    1,689

    Re: size of structure

    Quote Originally Posted by laserlight View Post
    Heheh, I think that this just confirms that these things are implementation defined, within the requirements set out by the C and C++ standards. Yet, egawtry did qualify with "most 32-bit machines (including PCs)", so he is not incorrect (other than the assumption that long will be 32-bit instead of 64-bit).
    Aye, but how many 32-bit machines are there left on the market? My company stopped supporting them all together a while ago.

    @aamir121a, most compiled have int16_t, int32_t and int64_t types defined. These are not standard C++, but they do guarantee you that they'll be 16, 32, and 64 bit respectively. (Also uint16_t, uint32_t, and uint64_t)

  14. #14
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: size of structure

    Quote Originally Posted by aamir121a View Post
    Further how important is structure alignment when using to read information to structure from binary file header.
    It is extremely important. If alignment handling in your code and in the file is different, they won't match.

  15. #15
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: size of structure

    thank you all , so it is ok to force structure alignment via pragma pack(1) , so the structure perfectly aligns with binary file header , any down side at all.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center