Consider the structure below:
struct DrawingBoard
{
bool m_bIsEditable;
float m_fXPos;
float m_fYPos;
int m_iOpacity;
bool m_bResizable;
short m_sSchema;
bool m_bCanExport;
float m_fBorderThickness;
bool m_bApplyCustomColor;
int m_iTransparency;
};
Its members are randomly organized (or rather organized basing on their functionality) without taking into account the way they packed into memory.
If we query the size of an object of this structure on a 32-bit OS (with packing of 8), then we will get 36 bytes.
DrawingBoard structDrawingBoard;
size_t n = sizeof( structDrawingBoard );
But if we group them based on their type and arrange them in increasing order of their size, then we COULD reduce the size:
struct DrawingBoard
{
bool m_bIsEditable;
bool m_bResizable;
bool m_bApplyCustomColor;
bool m_bCanExport;
short m_sSchema;
int m_iOpacity;
int m_iTransparency;
float m_fXPos;
float m_fYPos;
float m_fBorderThickness;
};
DrawingBoard structDrawingBoard;
size_t n = sizeof( structDrawingBoard );
Now the size of
structDrawingBoard
is only 28 bytes.
[We can still optimize by using bit fields or playing with pragma packing, BUT performace will be AFFECTED.]