C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
wrong size of strcut Posted by franktruijen on 15 Oct 2005 at 5:38 AM
Hello

I'm reading from a file steam with visual c++ 6.
I have declared the struct:

typedef struct {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} FileHeader;

When I do sizeof(FileHeader) the return value is 16 instead of 14.
I assume that this has something to do that visual c++ 6 is a 32 bit system. It is not possible to change the order inside the struct. Can anybody help me so the struct returns the correct value.

Kind regards,
Frank.
Report
Re: wrong size of strcut Posted by stober on 15 Oct 2005 at 5:47 AM
This message was edited by stober at 2005-10-15 5:50:50

: Hello
:
: I'm reading from a file steam with visual c++ 6.
: I have declared the struct:
:
: typedef struct {
: unsigned short bfType;
: unsigned int bfSize;
: unsigned short bfReserved1;
: unsigned short bfReserved2;
: unsigned int bfOffBits;
: } FileHeader;
:
: When I do sizeof(FileHeader) the return value is 16 instead of 14.
: I assume that this has something to do that visual c++ 6 is a 32 bit system. It is not possible to change the order inside the struct. Can anybody help me so the struct returns the correct value.
:
: Kind regards,
: Frank.
:


why would you expect 14 on a 32-bit compiler? sizeof(int) = 4, not 2 as in old MS-DOS C compilers like Turbo C. and sizeof(int) may be 8 on some 64-bit computers. You can't count on the sizeof of any data type being consistent from one compiler/os to another. The standards make no such guarentee, except sizeof(char) is always 1.


if you must make the sizeof the struct 14, then you will have to fiddle a little with the data types. Change the two ints to shorts and it should work -- the file system won't know the difference.


Report
Re: wrong size of strcut Posted by DB1 on 15 Oct 2005 at 6:32 AM
: This message was edited by stober at 2005-10-15 5:50:50

: : Hello
: :
: : I'm reading from a file steam with visual c++ 6.
: : I have declared the struct:
: :
: : typedef struct {
: : unsigned short bfType;
: : unsigned int bfSize;
: : unsigned short bfReserved1;
: : unsigned short bfReserved2;
: : unsigned int bfOffBits;
: : } FileHeader;
: :
: : When I do sizeof(FileHeader) the return value is 16 instead of 14.
: : I assume that this has something to do that visual c++ 6 is a 32 bit system. It is not possible to change the order inside the struct. Can anybody help me so the struct returns the correct value.
: :
: : Kind regards,
: : Frank.
: :
:
:
: why would you expect 14 on a 32-bit compiler? sizeof(int) = 4, not 2 as in old MS-DOS C compilers like Turbo C. and sizeof(int) may be 8 on some 64-bit computers. You can't count on the sizeof of any data type being consistent from one compiler/os to another. The standards make no such guarentee, except sizeof(char) is always 1.
:
:
: if you must make the sizeof the struct 14, then you will have to fiddle a little with the data types. Change the two ints to shorts and it should work -- the file system won't know the difference.
:
:
:





The size of the individual member variables adds up to 14 bytes.

The reason the sizeof(FileHeader) does not equal 14 is because of structure alignment. Your compiler is aligning the members to 4 or 8 byte boundaries.

In memory, your struct probably looks like this:
 typedef struct {
 	unsigned short	bfType;
        short padding; // 2 bytes of extra padding needed to align to 4 byte boundaries before the next member variable
 	unsigned int	bfSize;
 	unsigned short	bfReserved1;
 	unsigned short  bfReserved2;
 	unsigned int	bfOffBits;
 } FileHeader;


Re-arranging the struct to align on 4 byte boundaries will solve the problem.

Also, using the preprocessor packing directive to specify packing alignment will solve the problem.
#pragma pack(1)
 typedef struct {
 	unsigned short	bfType;
 	unsigned int	bfSize;
 	unsigned short	bfReserved1;
 	unsigned short  bfReserved2;
 	unsigned int	bfOffBits;
 } FileHeader;
#pragma pack



BTW, just because sizeof() returns 16 instead of the 14 you were expecting, doesn't mean it's wrong. It is correct. The compiler does things like this for optimization and faster execution.



To understand recursive, first you need to understand recursive

Report
Re: wrong size of strcut Posted by stober on 15 Oct 2005 at 8:03 AM
:
: The size of the individual member variables adds up to 14 bytes.

you are right -- I miscounted. I wouldn't make a very good compiler would I
Report
Re: wrong size of strcut Posted by AsmGuru62 on 16 Oct 2005 at 6:10 AM
: :
: : The size of the individual member variables adds up to 14 bytes.
:
: you are right -- I miscounted. I wouldn't make a very good compiler would I
:
The padding may not help, if you are reading the file written by old system - you need to read by its elements.
Report
Re: wrong size of strcut Posted by DB1 on 16 Oct 2005 at 4:51 PM
: : :
: : : The size of the individual member variables adds up to 14 bytes.
: :
: : you are right -- I miscounted. I wouldn't make a very good compiler would I
: :
: The padding may not help, if you are reading the file written by old system - you need to read by its elements.
:

#pragma pack(1) should work in this case




To understand recursive, first you need to understand recursive




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.