The function of the keyword sizeof is to return the number of bytes of memory consumed by the object or type, and the return value is size_t.
Number of bytes consumed by the base data type: (32-bit system)
Char 1 bytes
BOOL 1 bytes
Short 2 bytes
int 4 bytes
Long 4 bytes
Float 4 bytes
Double 8 bytes
One of the important factors affecting the sizeof result is byte alignment. First look at a formula: valid alignment value =min (self-aligning value, set alignment value).
The self-aligning value is the number of bytes that a data type itself occupies.
Example:
intA//the self-aligning value is 4CharC//the self-aligning value is 1DoubleD//the self-aligning value is 8//for structs, the self-aligning value is the highest value of the alignment value in its built-in data typestructs1{intQ//4 BOOLE//1 CharW//1};//the largest is the int q, so the self-aligning value is 4structs2{intQ//4 BOOLE//1 DoubleR//8};//the largest is double R, so the self-aligning value is 8
Setting the alignment value is the compiler default alignment value, the author uses vs2013, the default alignment value is 4 bytes, you can project--->properties--->configuration Properties--->c\c++--- >code Generation
--->struct Member alignment, or set by macro #pragma pack (n), n is the aligned byte to be set.
A valid alignment value is the smaller of the object or type's own alignment value and the set alignment value, and also the actual true alignment value.
After adding a few changes to the above example:
#pragmaPack (4)//set the alignment value to 4structs1{intQ;BOOLe;CharW;}; //S1 's self-aligning value is 4, so S1 's effective alignment value =min (+) =4#pragmaPack (2)//set the alignment value to 2structs2{intQ;BOOLe;DoubleR;}; //The S2 's own alignment value is 8, so S2 's valid alignment value =min (8,2) =2
Knowing the valid alignment values makes it easy to calculate sizeof.
One, the basic data type of sizeof
sizeof (int) =4
sizeof (double) =8
......
Second, the structure of the sizeof
#pragmaPack (4)structs1{intA;Charb;}; //The valid alignment value is 4sizeof(S1) =8 //int A is 4 bytes, stored in 0x00--0x03,char B for 1 bytes, stored in 0x04, because the valid alignment value is 4, so the 0x05--0x07 after Char b is aligned, taking up 8 bytes//change the S1 slightly .structs1{Charb;intA;}; //The valid alignment value is 4sizeof(S1) =8//the answer is the same, but the internal storage has changed. Char b occupies 1 bytes and is stored in 0x00,int A for 4 bytes, because the valid alignment value is 4,0x01---0x03 only 3 bytes of memory, less than the number of bytes required for int A, so a new allocation of 4 bytes (valid alignment value) of memory, the final int a stored in 0x04---0x07 //Look at one more example .structs2{CharA; Shortb;DoubleC;CharD;}; //The valid alignment value is 4sizeof(s2) = -//Char a accounted for 1 bytes, stored in 0x00;short b accounted for 2 bytes, because the valid alignment value is 4, the first memory remaining 3 bytes 0x01---0x03, greater than the short B required, so short b stored in 0x02---0x03 (note: The storage header must be an integer multiple of the member size, so 0x01 is empty); double c accounts for 8 bytes, which is greater than the valid alignment value, so allocate two memory 0x04---0x0b for storing double c;char d for 1 bytes, stored in 0x0c, aligned by a valid alignment value of 4 bytes, and the final 0x0d---0x0f. Altogether 16 bytes.
Three, the structure of the structure containing the type of sizeof
The self-aligning value of a struct is the largest of its built-in types.
Example:
#pragmaPack (2)classa{ Public: intA; Doubles; }; //the self-aligning value of a is the self-aligning value of double s, which is 8//but the valid alignment value of a is min (8,2) =2,sizeof (a) =12classb{ Public: CharC//self-aligning value 1, accounting for 1 bytesA b;//self-aligning value 8, sizeof (A) = 12 bytes};//The self-aligning value of B is equal to the self-aligning value of a, which is 8//valid alignment value for b =min (8,2) = 2, aligned by 2 bytessizeof(B) = - //char c occupies 1 bytes, is stored in 0x00,0x01, and A B occupies 12 bytes. Altogether 14 bytes.
Four, sizeof with virtual function in the structure body
The structure with virtual functions has a virtual table pointer, which accounts for 4 bytes in size.
Example:
#pragma Pack (4)public: int A; Virtual int test ();}; sizeof (a) =8 //int A is 4 bytes, virtual table pointer is 4 bytes, total 8 bytes. // Note: If a struct has more than one virtual function, there is still only one virtual table pointer, that is, multiple virtual functions share a virtual table pointer
In the case of inheritance, if the base class has a virtual function, then the virtual table pointer is also inherited, that is, the base class and derived classes share a virtual table pointer.
Example:
#pragmaPack (4)classa{ Public: intA; Doubles; Virtual inttest ();};classB: Publica{ Public: Virtual intTest_1 ();//Common base class virtual table pointers Virtual intTest_2 ();//Common base class virtual table pointers CharC; };sizeof(B) = - //int A is 4 bytes +double S is 8 bytes + Virtual table pointer accounted for 4 bytes +char C accounted for 1 bytes + 3 bytes is justified = 20 bytes
V. SizeOf, the Consortium
Each member of the Consortium shares memory, and sizeof, the entire consortium, is the maximum value of each member sizeof.
Example:
Union u { int A; Double b; Char C; BOOL D;}; sizeof (u) =sizeof(b) =8
VI. sizeof with a static structure
Both static and global variables are stored in a static storage area, and only non-static members are computed when sizeof computes a struct.
Example:
#pragma Pack (4)class a{public: int A; Double b; Static int // whatever he }; sizeof (a) = 4 bytes +double b 8 bytes =12
Seven, the function of sizeof
The result is the size of the function return type, so you cannot find a function that does not have a return value for sizeof.
Format sizeof (function name (argument table))
Example:
intA () {return 1;}sizeof(A ()) =sizeof(int)=4CharBCharb) { returnb;}sizeofB'b'))=sizeof(Char)=1voidC () {}sizeof(C ())//error, because there is no return type
Where there is a misunderstanding, I hope you will correct me!! Appreciate!
C + + sizeof summary