Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mits

sizeof() weird problem

This topic is 5221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Trying to load a .ms3d file I have in a header file this definition : struct SMs3DVertex { unsigned char m_ucFlags; float m_fVerts[3]; char m_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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
also, change the order and whenever you care about size, make it biggest to smallest

struct 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; 1byte
3 "empty" bytes so float is aligned correctly
float m_fVerts[3]; 12byte
char m_cBone; 1byte
unsigned 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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!