1. definition
sizeof is an operator (operator).
The function is to return the number of bytes of memory that an object or type occupies.
2. Syntax
There are three syntactic forms of sizeof:
1) sizeof (object); sizeof (object)
2) sizeof object; sizeof Object
3) sizeof (TYPE_NAME); sizeof (type)
objects can be variables of various types, as well as expressions (typically sizeof does not evaluate an expression).
sizeof makes the memory size of the object, ultimately converting it to evaluating the object's data type.
sizeof (expression); The value is the size of the data type of the end result of an expression
Example:
inti; sizeof(int);//Value of 4sizeof(i);//a value of 4, equivalent to sizeof (int)sizeofI//Value of 4sizeof(2);//a value of 4 is equivalent to sizeof (int), because 2 is of type intsizeof(2+3.14);//A value of 8 is equivalent to sizeof (double) because the result of this expression is of type doubleCharary[sizeof(int) *Ten];//OK, compile without error
1. SizeOf of the basic data type
The basic data Type here refers to simple built-in data types such as short, int, long, float, double.
Because their memory size is system-dependent, the values may be different under different systems.
2. sizeof of the structure
sizeof for structs involves byte alignment issues.
Why do I need byte alignment? The principle of computer composition teaches us that this helps to speed up the number of computers, otherwise we have to spend more instruction cycles. To do this, the compiler will handle the struct by default (as well as data variables elsewhere), so that the base data type of 2 (short, etc.) is located at an address divisible by 2, and the base data type (int, etc.) with a width of 4 is at an address divisible by 4, and so on. In this way, the number of bytes in the middle of the two may need to be added, so the sizeof value of the entire struct increases.
Byte alignment details are related to compiler implementations, but in general, three guidelines are met:
1) The first address of a struct variable can be divisible by the size of its widest base type member.
2) Each member of the struct is an integer multiple of the member size relative to the first address of the struct, and if necessary, the compiler adds padding bytes (internal adding) between the members.
3) The total size of the struct is an integer multiple of the size of the structure's widest base type member, and if necessary, the compiler adds padding bytes (trailing padding) after the last member.
Note: The sizeof value for an empty struct (without data members) is 1. Imagine how a "space-free" variable is addressed, and how the two different "empty struct" variables can be distinguished, so that the "empty struct" variable is stored so that the compiler can only allocate one byte of space for the placeholder.
Example:
structS1 {CharA; intb; }; sizeof(S1);//The value is 8, byte-aligned, and 3 bytes are populated after char. structS2 {intb; CharA; }; sizeof(S2);//The value is 8, byte-aligned, and 3 bytes are populated after char. structS3 {}; sizeof(S3);//the value is 1, and the empty structure also accounts for memory
3. SizeOf of the Consortium
Structs are listed sequentially in the memory organization, the consortium is overlapping, each member shares a piece of memory, so sizeof, the entire consortium, is the maximum value for each member sizeof.
Example:
Union u { int A; float b; Double C; Char D; }; sizeof // value of 8
4. The sizeof array
The sizeof value of the array equals the number of bytes of memory consumed by the array.
Note: 1) When a character array represents a string, its sizeof value evaluates to '/0 ' in.
2) When the array is a formal parameter, its sizeof value corresponds to the sizeof value of the pointer.
Example 1:
Chara[Ten]; CharN[] ="ABC"; cout<<"Char a[10]"<<sizeof(a) <<endl;//array with a value of tencout<<"char n[] =/"abc/" "<<sizeof(n) <<endl;//A string array that evaluates to '/0 ', with a value of 4
Example 2:
void func (char a[3]) { intsizeof//C = 4, because here A is not an array type, but a pointer, equivalent to char *a. } void FuncN (char b[]) { intsizeof //CN = 4 for the same reason.
5. The sizeof pointer
Pointers are used to record the address of another object, so the memory size of the pointer is of course equal to the width of the address bus inside the computer.
On a 32-bit computer, the return value of a pointer variable must be 4.
The sizeof value of a pointer variable has no relation to the object that the pointer refers to.
Example:
Char*b ="HelloWorld"; Char*c[Ten]; Double*D; int**e; void(*PF) (); cout<<"char *b =/"helloworld/" "<<sizeof(b) <<endl;//pointer to a string with a value of 4cout<<"Char *b"<<sizeof(*b) <<endl;//the pointer is pointing to a character with a value of 1cout<<"Double *d"<<sizeof(d) <<endl;//pointer with a value of 4cout<<"Double *d"<<sizeof(*d) <<endl;//the pointer points to a floating-point number with a value of 8cout<<"int **e"<<sizeof(e) <<endl;//pointer to pointer with a value of 4cout<<"Char *c[10]"<<sizeof(c) <<endl;//pointer array with a value ofcout<<"void (*PF) (); "<<sizeof(PF) <<endl;//function pointer with a value of 4
6. SizeOf for functions
sizeof can also evaluate a function call, and the result is that the function returns the size of the value type, and the function is not called.
form of evaluation of functions: sizeof (function name (argument table))
Note: 1) You cannot evaluate a function that has an empty return value type.
2) The function name cannot be evaluated.
3) In the case of a function with a parameter, when using sizeof, must write the argument table.
Example:
#include <iostream>using namespacestd; floatFUNCP (intAfloatb) {returnA +b; } intFUNCNP () {return 3; } voidFunc () {}intMain () {cout<<sizeof(FUNCP (3,0.4)) <<endl;//OK, Value 4,sizeof (FUNCP (3,0.4)) is equivalent to sizeof (float)cout<<sizeof(FUNCNP ()) <<endl;//OK, a value of 4,sizeof (FUNCNP ()) is equivalent to sizeof (int)/*cout<<sizeof (Func ()) <<endl;//error,sizeof cannot evaluate a function that returns a null type*/ /*cout<<sizeof (FUNCNP) <<endl;//error,sizeof cannot evaluate function names*/ return 0; }
Introduction to the usage of sizeof () in C + +