definition: sizeof is the number of bytes that are used to compute objects, usually to view the operands of a variable, array, or struct, and so on.form: sizeof () has three syntactic forms: 1 for data types: sizeof (type_name);//sizeof (type); 2) for variables: sizeof (object);//sizeof (objects); sizeof object;// sizeof object; example: void Main () {int a=10; int x=sizeof (a);√x=sizeof (int);√X=sizeof A;√x=sizeof int; X}
The expression of the first three types of sizeof () in the above four grammatical forms is correct, the fourth is wrong, and the type must be bracketed. Note: The sizeof operator cannot be used for function types, incomplete types, or bit fields. features and usage : 1) sizeof is an operator, not a function; 2 sizeof cannot find the length of a void type; 3) sizeof the length of the pointer to a void type; 4) sizeof the length of the array that can obtain the statically allocated memory Note: Use sizeof to find the length of the string with the '/0 ' example: void fun (int array[10]) { int n=sizeof (array); return n; Run Result: 4 The value of the Fun class n is 4, instead of 40, this is because when the function parameter passes, the array is converted to a pointer, and if the entire array is passed directly, then the copy of the array element (the argument to the copy of the formal parameter) is necessarily involved, and when the array is very large, the function performs extremely inefficiently. Instead, only the address of the array (that is, the pointer) is passed, and only 4byte copies are required. 5) sizeof cannot be length to an incomplete array, or it will compile an error; 6 when an expression is a sizeof operand, it returns the type size of the expression's evaluated result, but it does not evaluate the expression; 7) sizeof can size the function call. And the size of the return type (function type) is equal to the size of the but not the function body; 8 the size of the structure body (and its object) sizeof is not equal to the size of each data member object; rule: A, the size of the structure is equal to the size of the maximum member of the structure body; B, the first address of a member in a structure body is an integer multiple of its type, relative to the first address of the struct body. For example, the address offset of a double member relative to the first address of the structure body should be a multiple of 8; C, to satisfy the rules A, B, The compiler is populated with bytes after the members of the struct body, for example: #include using namespace std; int main () { struct A {& nbsp; inT num1=0; int name2=0; double num3=0; }; struct B { int n1=0; double n2=0; int n3=0; }; cout<< "a=" <<sizeof (A) <<endl; cout<< "b=" <<sizeof (B) <<endl; System ("pause"); return 0; Run Result: a=16 b=24//Analysis: sizeof (A): 4+4+8=16 sizeof (B): 4 (N1 footprint) +4 (not more than 8 fill 4 address space) +8 (N2 footprint) +4 (N3 occupy address space) +4 (not enough 8 to fill 4 address space) =24; 9) sizeof cannot be used to find the size of the bit-domain members of the structure, However, the size of the structure containing the bit domain members can be obtained.