wrong size of strcut

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.

Comments

  • [b][red]This message was edited by stober at 2005-10-15 5:50:50[/red][/b][hr]
    : 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.


  • : [b][red]This message was edited by stober at 2005-10-15 5:50:50[/red][/b][hr]
    : : 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:
    [CODE]
    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;
    [/CODE]

    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.
    [CODE]
    #pragma pack(1)
    typedef struct {
    unsigned short bfType;
    unsigned int bfSize;
    unsigned short bfReserved1;
    unsigned short bfReserved2;
    unsigned int bfOffBits;
    } FileHeader;
    #pragma pack
    [/CODE]


    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.



    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • :
    : 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 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 :-)
    :
    [blue]The padding may not help, if you are reading the file written by old system - you need to read by its elements.[/blue]
  • : : :
    : : : 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 :-)
    : :
    : [blue]The padding may not help, if you are reading the file written by old system - you need to read by its elements.[/blue]
    :

    #pragma pack(1) should work in this case




    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories