Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 21st, 2007, 11:20 PM   #1
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
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.
Seif is offline   Reply With Quote
Old May 22nd, 2007, 1:53 AM   #2
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
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.
__________________
os: mac os 10.5.4
revision control: git
editor: emacs

site
Mad_guy is offline   Reply With Quote
Old May 22nd, 2007, 6:50 AM   #3
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
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!
rsnd is offline   Reply With Quote
Old May 22nd, 2007, 7:34 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old May 24th, 2007, 11:36 AM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
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.
Seif is offline   Reply With Quote
Old May 24th, 2007, 1:49 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:51 AM.

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