There is something called packing alignment which is applied for structure, union, and class members. This alignment can be specified to the compiler by using #pragma instruct.
Now for your compiler I guess the default packing alignment is 4. So once that 4 bytes are exhausted, then the compiler will go for 4 bytes instead of the exact size of the member.
One should be careful while modifying packing alignment as this may effect performance.
Now a struct with members
#pragma pack(4)
struct A
{
short m_s1;
int* m_pi;
short m_s2;
};
void main()
{
A obj;
size_t s = sizeof(obj);
}
Here sizeof(obj) is 12. Now we can reduce this to 8 bytes by changing the order of structure members
struct A
{
short m_s1;
short m_s2;
int* m_pi;
};