Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   struct gaining 2 bytes ? (http://www.programmingforums.org/showthread.php?t=13192)

Seif May 22nd, 2007 12:20 AM

struct gaining 2 bytes ?
 
Hi, I'm having the problem when trying to read in the data to the following struct.

:

struct BMPFileHeader
{
        unsigned char        fileType[2];
        long                        fileSize;
        unsigned short        reserved[2];
        long                        dataOffset;
};


now, this struct should only be 14 bytes, but when doing a sizeof of this header, i find that it is giving 16 bytes instead, causing some trouble when reading data.

I remember reading why this happens a while back. but I was wondering, is there any work around ? I would like to keep the struct if possible for ease of coding, but I can read em in individually if need be.... sigh.

I can't change the order of which I declare these either.

Mad_guy May 22nd, 2007 2:53 AM

iirc, your compiler will generally pad your structures to an 8-byte boundry, i.e. it'll pad it 'up to' 16.

When I manipulated the PE header, the general way I did it was just make a pointer to the structure and just point it at the start of your file map; pointer arithmetic should take care of it from there (the code to manipulate/read PE files I wrote I still have, you can see it here if you want a reference.)

I don't quite know how you're trying this, though. Have you tried it with the existing structure? If so, providing there were issues, it'd be nice to see some example code.

rsnd May 22nd, 2007 7:50 AM

I think most compilers these days including vc++ comes with the #pragma pack preprocessor directive.
sample reference: http://publib.boulder.ibm.com/infoce...f/rnpgpack.htm

Set it to 1 to solve all your problems.

Think there is also the "UNALIGNED" type modifier supported by some compilers

DaWei May 22nd, 2007 8:34 AM

If you're working with Windows, I'd recommend you skip the packing and use their predefined stuff for bitmap handling. The bitmap file header, for instance, is
:

typedef struct tagBITMAPFILEHEADER {
  WORD    bfType;
  DWORD  bfSize;
  WORD    bfReserved1;
  WORD    bfReserved2;
  DWORD  bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;

This would allow you to use some of their predefined bitmap functions.

Seif May 24th, 2007 12:36 PM

cheers for the response. I decided to read each part of the struct in seperately as it is only 5 parts in size. Am working with windows, but really wanted to use my own code.... I know its reinventing the wheel.

I currently have both the file and info header reading in ok, but I am now finding difficulty reading in the actual image data. But alas, I believe I should leave that for another thread.

DaWei May 24th, 2007 2:49 PM

You're aware that the data may either contain the pixel values or be an index into a color table?


All times are GMT -5. The time now is 2:24 AM.

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