Thread: Problem with structs

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Problem with structs

    Hi all,

    I am trying to write a bmp image to a bmp file.

    I can get it to work as long as i use the structs provided by the windows.h header. However if i define the structs myself it breaks my code.

    It is one struct in particular, if i use the windows.h BITMAPFILEHEADER my program works fine. However if i use this struct it does not work:

    Code:
    struct BITMAPFILEHEADER
    {
      unsigned short bfType;
      unsigned long bfSize;
      unsigned short bfReserved1;
      unsigned short bfReserved2;
      unsigned long bfOffBits;
    };
    i've found that the windows struct is 14 bytes in size and my struct is 16 bytes in size and i think that this is the issue. However, i cannot understand why my struct is 2 bytes larger?

    Any advice would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Code:
    #pragma pack(push,2)
    typedef struct tagBITMAPFILEHEADER {
        WORD    bfType;
        DWORD    bfSize;
        WORD    bfReserved1;
        WORD    bfReserved2;
        DWORD    bfOffBits;
    } BITMAPFILEHEADER,*LPBITMAPFILEHEADER,*PBITMAPFILEHEADER;
    #pragma pack(pop)
    Notice the pragmas around the definition? It means the struct will align to 2-byte boundary while yours will have an extra 2 bytes because of the machine 4-byte alignment. To solve this in GCC you can use the pack() pragma respectively or use the handy "packed" attribute like this:

    Code:
    struct BITMAPFILEHEADER
    {
      unsigned short bfType;
      unsigned long bfSize;
      unsigned short bfReserved1;
      unsigned short bfReserved2;
      unsigned long bfOffBit;
    } __attribute__((packed));
    Last edited by hauzer; 06-03-2009 at 03:39 PM.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Smile

    Hauzer,

    Many thanks for the resolution, my program now compiles and runs without issue.

    I am just learning c++ and do not understand exactly what is happening when using pragma or the packed attribute you have advised. I am guessing it might be an advanced topic that is beyond me at this time. However if you have the time and are able to explain what is happening so a novice can understand, i would greatly appreciate the input.

    Again, thanks for the solution.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    No problem, and welcome to the forums.
    Since you're a beginner, and I don't know what knowledge you have, I'm not quite sure if you'll get what I'm talking about. You need to have a good grasp of pointers and memory.

    Variables in 32bit environments have to be in memory addresses divisible by 4, or else it will generate an error, some processors are not bound by this rule, but require performance hits when not doing so. When a structure, like the one provided, has a size not divisible by 4 the compiler inserts a dummy variable into it so it is. The structure would then look something like this:

    Code:
    struct BITMAPFILEHEADER
    {
      unsigned short bfType;
      unsigned long bfSize;
      unsigned short bfReserved1;
      unsigned short bfReserved2;
      unsigned short dummy; //Notice this
      unsigned long bfOffBit;
    };
    So when we use the attribute packed we tell the compiler it shouldn't insert the dummy variable and leave it as it is. This requires more processing power, so I'm not sure if it's a good trade-off.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Hauzer,

    Thanks for the explanation, it made perfect sense. I have recently gone though dynamically allocating memory and therefore i am familiar with pointers and memory but was unaware of this issue with structs.

    This learning curve is certainly a steep one

    Thanks for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problem with structs and functions
    By Moose112 in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2006, 11:04 PM
  3. Problem with array of structs
    By Wiretron in forum C Programming
    Replies: 3
    Last Post: 09-03-2006, 02:07 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. register structs - problem declaring
    By Kagey in forum C Programming
    Replies: 11
    Last Post: 11-11-2002, 03:58 AM