http://www.ibm.com/developerworks/library/pa-dalign/
To determine the size of a class/struct is important in programming with C++. A instant example of this issue is to calculate the pointer of the next item in an array of objects (adding the offset value to the initial pointer). The mechanism of determining the size comes from the rules of C, and expand to that of C++. The basic idea is to find out a calculating unit (which should be a multiple of 4, i.e., 1, 2, 4, 8, 16, ...), and then read in a unit each time until the .
The reason why C/C++ use this method is to sped up the read in. Use a (calculating) unit will save the time of calculating address.
Essentially, sizeof is an operator but not a function!!!It will return zero of an array which is not declared as a fixed size.
Description:
1. Find the largest system defined variable, and set it as the read-in unit.
2. The program will read in data based on the unit. (i.e. if double (8 bytes) is a unit, every time the program will readin 8 bytes.)
3. If a variable is not coincides with multiplicity of the unit, make up some bytes. The make-up bytes will be discarded after being read in. (i.e. char name[7]; is of size 7, and the unit is 8. Then, make up 1 byte so as to they can be read in once.)
4. About Member functions: member functions will not take size of a struct/class.
5. About "virtual" method: C++ will add a pointer (ess. an integer taking 4 bytes) to each virtual function; C++ will add a pointer to the base class, if the inherience is virtual.
6. Use #pragma pack(n) to set a new alignment method. (0, default method; 1 one byte; and so on).
Example code:
typedef struct test_struct{
int m;
int m_1;
int m_2;
double n;
char name[7];
} test_struct;
class BaseClass
{
public:
BaseClass(int input) {}
BaseClass(float input) {}
BaseClass(double input) {}
~BaseClass() {}
virtual void Print() {}
private:
double m_double;
int m_int;
int m_int_1;
char name[11];
};
class DerivedClass : public BaseClass{
private:
int d_m;
};
class DerivedClassVirtual : virtual public BaseClass{
private:
int d_m;
};
class Wrapper{
private:
DerivedClass m_object;
int m_size;
};
int main()
{
enum eDay {Mon = 1, Tue};
cout << "Size of bool = " << sizeof(bool) << endl; //1 bit, take the lowest position of a 8-bit array.
cout << "Size of char = " << sizeof(char) << endl;
cout << "Size of int = " << sizeof(int) << endl;
cout << "Size of long = " << sizeof(long) << endl;
cout << "Size of float = " << sizeof(float) << endl;
cout << "Size of double = " << sizeof(double) << endl;
cout << "Size of long double = " << sizeof(long double) << endl;
cout << "Size of char array with 10 elements = " << sizeof(char[10]) << endl; //=sizeof(char) (=1) * 10
cout << "Size of int array with 10 elements = " << sizeof(int[10]) << endl; //=sizeof(int) (=4) * 10
cout << "======================================" << endl;
cout << "Size of struct = " << sizeof(test_struct) << endl;
cout << "Size of enum = " << sizeof(eDay) << endl;
cout << "Size of base class = " << sizeof(BaseClass) << endl;
cout << "Size of derived class = " << sizeof(DerivedClass) << endl;
cout << "Size of virtual derived class = " << sizeof(DerivedClassVirtual) << endl;
cout << "Wrapper size = " << sizeof(Wrapper) << endl;
return 0;
}
Sample output:
Size of bool = 1
Size of char = 1
Size of int = 4
Size of long = 4
Size of float = 4
Size of double = 8
Size of long double = 12
Size of char array with 10 elements = 10
Size of int array with 10 elements = 40
======================================
Size of struct = 32
Size of enum = 4
Size of base class = 40
Size of derived class = 40
Size of virtual derived class = 48
Wrapper size = 48