Hi there, i got a question about structure's size, here's the destription:

in file Windows.inc, there's a structure like this:

CHARFORMATA STRUCT
cbSize DWORD ?
dwMask DWORD ?
dwEffects DWORD ?
yHeight DWORD ?
yOffset DWORD ?
crTextColor COLORREF ?
bCharSet BYTE ?
bPitchAndFamily BYTE ?
szFaceName BYTE LF_FACESIZE dup(?)
_wPad2 WORD ?
CHARFORMATA ENDS

and in Windows.h, it's like this:

typedef struct _charformat {
UINT cbSize;
DWORD dwMask;
DWORD dwEffects;
LONG yHeight;
LONG yOffset;
COLORREF crTextColor;
BYTE bCharSet;
BYTE bPitchAndFamily;
TCHAR szFaceName;
} CHARFORMAT;


notice that in windows.inc, has this line, and it hasn't appeared in windows.h :

_wPad2 WORD ?



Then, i compile the following source in MASM and VC++ :

MASM:
mov cft.cbSize, sizeof CHARFORMAT

VC:
cbSize=sizeof(CHARFORMAT);


then disassemble them, i found that they have the SAME SIZE!

So anyone can tell me why this two STRUCTURES will have the same SIZE?


thanks a lot!:stupid:

(Sorry for my poor English.....)
Posted on 2003-02-05 09:59:06 by pazuluo
the C compiler does the padding for DWORD aligned structures automatically.
for (M)ASM you need to do this yourself by adding the 2 bytes padding.

Normally all Windows structs are already aligned, there very few examples,
like the CHARFORMAT struct.
Posted on 2003-02-05 12:24:07 by beaster

the C compiler does the padding for DWORD aligned structures automatically.
for (M)ASM you need to do this yourself by adding the 2 bytes padding.

Normally all Windows structs are already aligned, there very few examples,
like the CHARFORMAT struct.


Thank you beaster! :)

So you mean all MASM structures must padding for DWORD alignment, it solves my problem, thanks!
Posted on 2003-02-05 19:30:00 by pazuluo
So does the auto padding of C mean that it is more in tune with the subsytem?

This padding has something to do with keeping with the alignment correct does it not. To end on a double word boundary.....as in the example to pad a word?
Posted on 2003-02-05 22:39:23 by IwasTitan
All structs should be aligned to Dword..
Posted on 2003-02-06 07:29:57 by roticv