   sizeof() weird problem
  Trying to load a .ms3d file  I have in a header file this definition :struct SMs3DVertex{   unsigned char m_ucFlags;   float    m_fVerts[3];   charm_cBone;   unsigned char m_ucUnused;};If in my main.cpp try this: FILE *f = fopen("log.txt","w");fprintf(f,"%d\n",sizeof(SMs3DVertex));fclose(f);I get in the file the number 20 which is not right since the size of the structure is 15! If I try it with other data types(like float or int) I get the right value. What''s wrong? Has anyone any idea?Has anything to do with compiler settings?Please help!!! Thanks in advance 
  Your compiler is padding the struct with extra bytes to align it to a certain boundary; you can disable this / change the alignment according to your compiler settings. ;  I suppose it''s trying to align to 4-byte boundaries, so although m_ucFlags takes up only 1 byte, m_fVerts[3] needs to start 4 bytes after the starting point of m_ucFlags, thus 3 bytes are empty space.  The same occurs for m_cBone and m_ucUnused.  If you''re using VC++ 6, check out the #pragma pack(n) directive.  That will allow you to change how the padding works for specific structures, and not your entire program in general.  There might be similar commands for other compilers, but I wouldn''t know.  Or you could just set the program-wide packing method, if it isn''t a problem.  However, only do so if you definitely know why you want your structure to be smaller, because it could potentially affect your performance negatively. ;  Thanks for the answers! ;  also, change the order and whenever you care about size, make it biggest to smalleststruct SMs3DVertex{float m_fVerts[3];unsigned char m_ucFlags;unsigned char m_ucUnused;char m_cBone;};that should be 15 + 1byte padding = 16byte.what you have now:unsigned char m_ucFlags; 1byte3 "empty" bytes so float is aligned correctlyfloat m_fVerts[3]; 12bytechar m_cBone; 1byteunsigned char m_ucUnused; 1byte-> 18 byte, whole struct needs 4 byte alignment, so +2 bytes padding   [edited by - Trienco on February 8, 2004 3:30:08 PM]