![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You're aware that the data may either contain the pixel values or be an index into a color table?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finding file size | thechristelegacy | PHP | 8 | Feb 4th, 2007 12:35 AM |
| How to create struct containing variable array struct | shoeyfighter | C | 14 | Nov 6th, 2006 3:26 AM |
| Casting a struct to s aimilar struct | shoeyfighter | C | 6 | Oct 27th, 2006 2:59 PM |
| Ackerman's function - crash upon launch | codylee270 | C++ | 6 | Oct 19th, 2006 2:14 AM |
| Appreciate comments on some linked list code | Jessehk | C | 5 | Jul 20th, 2006 7:59 PM |