Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   sizeof() returns "wrong" Size of structure (https://forum.exetools.com/showthread.php?t=7965)

Cobi 08-19-2005 06:36

sizeof() returns "wrong" Size of structure
 
Why is this structure 20 Bytes in size?
Code:

typedef        struct STGAHeader{

        BYTE        m_uIDSize;
        BYTE        m_uColorMapType;
        BYTE        m_uImageType;

        short        m_sColorMapStart;
        short        m_sColorMapLength;
        BYTE        m_uColorMapBits;

        short        m_sXStart;
        short        m_sYStart;
        short        m_sWidth;
        short        m_sHeight;
        BYTE        m_uBits;
        BYTE        m_uDescriptor;

} STGAHeader;

(BYTE = unsigned char = 1 byte, short = 2 bytes)
For me 1+1+1+2+2+1+2+2+2+2+1+1 is 18 and not 20 but the compiler thinks different :confused:
Its absolutely impossible that 6*integer1+6*integer2 is equal 20
(Intel 8.0 win)
thx, Cobi

tom324 08-19-2005 06:49

Try with

#paragma pack(1) (or Intel 8.0 equivalent) directive to have alignment set to 1.

Tom

Cobi 08-19-2005 07:04

Seems to work, thx.
Guess i need to learn my tools :o

Mkz 08-19-2005 16:51

Just to make sure you know the *reason*:

After "m_uImageType" and before "m_sColorMapStart", the compiler inserted a padding "0", so that the 2-byte-wide "m_sColorMapStart" will be stored at an address which is a multiple of 2. If you had a DWORD, the compiler would pad with zeroes until the address would be a multiple of 4, and so on.

The pragma directive will tell the compiler not to bother aligning the structures.

If you follow the address of each structure member and add the length of the padding zeroes where they are "needed", you'll see that the total length is 20.

infern0 08-19-2005 17:53

Quote:

Originally Posted by tom324
Try with

#paragma pack(1) (or Intel 8.0 equivalent) directive to have alignment set to 1.

Tom

the best way is:
#pragma pack(push, 1)
struct ...
{};
#pragma pack(pop)

to avoid pack problems with other structures :)

JuneMouse 08-19-2005 19:11

could be a good reading
http://blogs.msdn.com/larryosterman/archive/2005/04/07/406252.aspx
there is a part 2 too

oxagen 08-20-2005 06:29

Quote:

Originally Posted by infern0
the best way is:
#pragma pack(push, 1)
struct ...
{};
#pragma pack(pop)

to avoid pack problems with other structures :)

Another way
#include <PshPack1.h>
struct ...
{};
#include <PopPack.h>


All times are GMT +8. The time now is 08:08.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX