Sizeof is an operator that returns the memory bytes occupied by an object or data type.
Instance
[Cpp]
# Include "stdafx. h"
# Include <stdlib. h>
# Include <iostream>
Using namespace std;
Void * Fun1 ()
{
Printf ("void \ n ");
Return NULL;
}
Char Fun2 ()
{
Char c = 'C ';
Printf ("char c = 'C '");
Return c;
}
Int Fun3 ()
{
Int I = 2;
Printf ("int I = 2 ");
Return I;
}
Struct strc0
{
};
Struct strc00
{
Int getA ();
};
Struct strc000
{
Int * getA ()
{
Return NULL;
}
};
Struct strc1
{
Int;
Char B;
};
Struct strc2
{
Char B;
Int;
};
Struct strc3
{
Char B;
Int;
Double c;
};
Struct strc4
{
Double c;
Char B;
Int;
};
Struct strc5
{
Char B;
Double c;
Int;
};
Struct strc6
{
Int;
Double c;
Char B;
};
Struct strc7
{
Int;
Double c;
Char B;
Char d;
};
Class classA0
{
};
Class classA00
{
Int GetA ();
Char GetB ();
};
Class classA000
{
Int GetA ()
{
Return 1;
}
Char GetB ()
{
Return 'C ';
}
};
Class classA1
{
Int;
Double B;
};
Class classA2
{
Char B;
Double c;
Int;
};
Class classA3
{
Char B;
Double c;
Int;
Char d;
};
Int _ tmain (int argc, _ TCHAR * argv [])
{
// Basic Data Type
Printf ("% d \ n", sizeof (char); // 1
Printf ("% d \ n", sizeof (int); // printf ("% d \ n", sizeof (2); 4
Printf ("% d \ n", sizeof (double); // printf ("% d \ n", sizeof (2.12); 8
Printf ("% d \ n", sizeof (string); // 32
// Function: the return value is independent of the internal applied variable and does not call the function.
Printf ("% d \ n", sizeof (Fun1 (); // 4 // no function called
Printf ("% d \ n", sizeof (Fun2 (); // 1
Printf ("% d \ n", sizeof (Fun3 (); // 4
// Pointer: 4 bytes in memory
Printf ("% d \ n", sizeof (void *); // 4
Printf ("% d \ n", sizeof (int *); // 4
// Array: its value is (subscript * Data Type). When the array is a parameter, it becomes a pointer.
Int a1 [4];
Char a2 [] = "abcd"; // char a2 [4] = "abcd"; // array limit Overflow
Char a3 [6] = "abcd \ 0"; // description "\ 0" is a character (Escape Character)
Char a4 [20] = "abc ";
Printf ("% d \ n", sizeof (a1); // 4*4 = 16
Printf ("% d \ n", sizeof (a2); // 5, there is a NULL ending character at the end of the character
Printf ("% d \ n", sizeof (a3); // 6
Printf ("% d \ n", sizeof (a4); // 20, indicating that 20 bytes of memory is allocated to a4, whether or not it is full
Printf ("% d \ n", sizeof (a4 [10]); // 1, a4 [4] is a char type data
// Struct, which involves the issue of byte alignment. The details of byte alignment are related to the compiler implementation.
// The total size of the struct is an integer multiple of the size of the widest basic type of the struct.
// The offset of each member of the struct to the first address of the struct is an integer multiple of the member size.
// Sum up by yourself: the size of the struct = the size of the widest basic type member + the number of its before and after measurement. Its member functions do not occupy memory.
Printf ("% d \ n", sizeof (strc0); // 1. The empty struct occupies 1 of the memory.
Printf ("% d \ n", sizeof (strc00); // 1. Functions in the structure do not occupy memory.
Printf ("% d \ n", sizeof (strc000); // 1, the function defined in the structure does not occupy memory, no matter what the return value is.
Printf ("% d \ n", sizeof (strc1); // 8, indicating character alignment in the struct
Printf ("% d \ n", sizeof (strc2); // 8
Printf ("% d \ n", sizeof (strc3); // 16 instead of 24
Printf ("% d \ n", sizeof (strc4); // 16 instead of 24
Printf ("% d \ n", sizeof (strc5); // 24 instead of 16
Printf ("% d \ n", sizeof (strc6); // 24 instead of 16
Printf ("% d \ n", sizeof (strc7); // 24
// Class: Same as struct. Class size = the size of the widest basic type member + the number of its before and after measurement. And member functions do not occupy memory
Printf ("% d \ n", sizeof (classA0); // 1
Printf ("% d \ n", sizeof (classA00); // 1, indicating that the declared function does not occupy the memory
Printf ("% d \ n", sizeof (classA000); // 1, indicating that the defined function does not occupy memory
Printf ("% d \ n", sizeof (classA1); // 16, indicating that character alignment also exists in the class
Printf ("% d \ n", sizeof (classA2); // 24
Printf ("% d \ n", sizeof (classA3); // 24
System ("pause ");
Return 0;
}
Conclusion:
Class/struct size = the widest member type + the Number of pre and post measurements. In addition, member functions do not occupy memory, regardless of whether they return values.