Why does size of structure show greater than total size of its members?
This is a because of padding done by the compiler to reduce the access time of structure members, and thus increase the efficiency of your program. Most CPUs perform best when basic types like int, char, float etc. are aligned in memory boundaries of particular size. Usually on 16bit architectures, the objects are aligned at 2 bytes boundary, and on 32bit archetectures, objects are aligned at 4 bytes boundary.
Example
Suppose there is an int object declared in structure as below:
struct myStruct
{
char b;
int a;
};
For implementation having 4 bytes boundary, the compiler would add 3 byte after
b, so that the int object
a lies in the 4 bytes boundary. If
b gets an address of 2000, the compiler would put
a at the location 2004, leaving the locations 2001, 2002 and 2003 as padding bytes. Hence the size of this struct may not be the total size of its members.