CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: size of structure

    and pragma pack(1) works with MINGW as well to will try this for GCC under linux

  2. #17
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: size of structure

    Quote Originally Posted by aamir121a View Post
    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.
    Of course, there is a downside. Otherwise, why do you think the default option would be to waste memory?

    If you need to read a struct from a binary input, then read the elements one by one. Packing may give you a small performance gain on reading/writing (which is slow anyway), but cost performance on other operations (which are typically not slow).

    Also, you should carefully consider whether the binary representation of a type will always be the same. The posts above show that this is not the case for different compilers / platforms. Unless you can exclude such cases, I wouldn't advice using binary files.
    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. #18
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: size of structure

    Be careful with pack(1) or __align; A lot of processors do not allow mis-aligned ints. ARM, Blackfin, SHARC, and several others will crash if you don't have the BYTE, WORD, or DWORD on a 16 bit boundary.

    By the way, all MS Windows compilers:

    char = _int8_t = 8 bit
    short = _int16_t = 16 bit
    long = _int32_t = 32 bit
    long long = _int64_t = 64 bit

    ----------------

    -Erik

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

    Re: size of structure

    I'm confused why you're writing your own bitmap loader anyway. There are tons of them out there for both loading standard bitmaps and windows bitmaps. They will do memory alignment, row padding, header loading and validating all for you. Bitmaps are weird. For instance, if the header says that the bitmap is 3 bytes per pixel, and it's 10 pixels wide, how many bytes is one row of the bitmap in the file? I'll give you a hint, it's not 30.

  5. #20
    Join Date
    Feb 2002
    Posts
    4,640

    Re: size of structure

    FYI, the only guarantee is:
    Code:
    sizeof (char) = 1
    sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long)
    As stated in the C++ specs. For some compilers, these are all '1'.

    Viggy

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: size of structure

    Quote Originally Posted by MrViggy View Post
    FYI, the only guarantee is:
    Code:
    sizeof (char) = 1
    sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long)
    As stated in the C++ specs. For some compilers, these are all '1'.

    Viggy
    The only guarantees with respect to size in bytes, yes. However, it is not the only guarantee when we include range. For a standard conforming implementation on which sizeof(char) == sizeof(long), it implies that the range of char would be at least [-2147483647, 2147483647] if it were signed, or [0, 4294967295] if it were unsigned. I find that rather unlikely, though certainly possible.
    Last edited by laserlight; August 10th, 2010 at 11:29 AM.
    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

Page 2 of 2 FirstFirst 12

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

Featured