The following code uses the platform Windows7 64bits+vs2012.
sizeof is an operator (operator) in C/s + + that returns the number of bytes of memory that an object or type occupies, frequently used, and has a thorough understanding of the need for alignment.
Basic syntax for 1.Sizeof
sizeof has three grammatical forms, as follows:
(1) sizeof (object); sizeof (object);
(2) sizeof (TYPE_NAME); sizeof (type);
(3) sizeof object; sizeof object;
The third grammatical structure, although correct, is simple and uniform, using the one or two notation. For example:
int i;sizeof( i ); // oksizeof i; // oksizeofint// oksizeofint; // error
2.sizeof computing basic types and expressions
sizeof computes the size of the object to be converted to the calculation of the object type, that is, the sizeof values of different objects of the same type are consistent. Here, the object can be further extended to the expression, that sizeof can evaluate an expression, the compiler determines the size based on the final result type of the expression, and sizeof is the compile-time operation, independent of the runtime, and does not evaluate the expression.
Examine the following code:
#include <iostream>using namespace STD;intMainintargcChar* argv[]) {cout<<"sizeof (char) ="<<sizeof(Char) <<endl;cout<<"sizeof (short) ="<<sizeof( Short int) <<endl;cout<<"sizeof (int) ="<<sizeof(int) <<endl;cout<<"sizeof (long) ="<<sizeof(Long int) <<endl;cout<<"sizeof (long) ="<<sizeof(Long int int) <<endl;cout<<"sizeof (float) ="<<sizeof(float) <<endl;cout<<"sizeof (double) ="<<sizeof(Double) <<endl;intI=8;cout<<"i="<<i<<endl;cout<<"sizeof (i) ="<<sizeof(i) <<endl;cout<<"sizeof (i) ="<<sizeof(i=5) <<endl;cout<<"i="<<i<<endl;}
Running results under 64bits windows is
sizeof (char) =1
sizeof (short) =2
sizeof (int) =4
sizeof (long) =4
sizeof (long Long) =4
sizeof (float) =4
sizeof (double) =8
I=8
sizeof (i) =4
sizeof (i) =4
I=8
Note two points,
(1) The value of I does not change, indicating that the expression within the sizeof brackets is not executed, and sizeof evaluates the type of the result of the expression at compile time, and the sizeof operation is independent of the runtime. sizeof (i) is equivalent to sizeof (int), and sizeof (I=5) is equivalent to sizeof (int), meaning that in the executable code, it does not contain the expression i=5, which was processed early in the compilation phase.
(2) whether long int is 8 bytes, related to compiler implementation, Visual C + + compiler used in VS2012 is Cl.exe, under 64bits windows will still compile a long to 4 bytes, to use a 8-byte long integer, for insurance purposes, use long Long type.
3.sizeof Calculating pointer variables
The pointer is the soul of C, C + +, which records the address of another object. Since it is to store the address, it certainly equals the width of the internal address bus of the computer. Therefore, in a 32-bit computer, the return value of a pointer variable must be 4 (in bytes), it can be deduced that in the future 64-bit system pointer variable sizeof result is 8.
Char* PC ="ABC";int* pi=New int[Ten];string* PS;Char* * PPC = &pc;void(*PF) ();//function pointerCharTestFunc () {return' K ';}sizeof(PC);//result is 4sizeof(PI);//result is 4sizeof(PS);//result is 4sizeof(PPC);//result is 4sizeof(PF);//result is 4sizeof(&testfunc);//result is 4sizeof(TestFunc ());//result is 1sizeof(* (TestFunc) ());//result is 1
Examine the above code and draw the following conclusions:
(1) The sizeof value of the pointer variable has no relation to the object type that the pointer refers to, it has no relation to how much space the pointer is requesting, and all the pointer variables occupy the same amount of memory. Then why in the native 64bits system, the pointer variable size is still 4 bytes, because using the 32-bit compiler compiled to get the program is 32 bits, so the pointer size is 4 bytes, you can modify the compiler version, no longer repeat.
(2) &testfunc represents a function pointer, the pointer size is 4, so sizeof (&testfunc) ==4. TestFunc () represents a function call, and the return value type is char, so sizeof (TestFunc ()) ==sizeof (char) ==1. The TestFunc name itself is a function pointer, so (*TESTFUNC) () is also a function call, sizeof ((*testfunc) ()) ==sizeof (char) ==1.
4.sizeof computed Array
When sizeof acts on an array, it takes the size of all the elements in the array. Refer to the following code:
inta[3][5];Charc[]="123456";Double* (*D) [3][6];cout<<sizeof(A) <<endl;//Output cout<<sizeof(a[4]) <<endl;//Output cout<<sizeof(a[0][0]) <<endl;//Output 4 cout<<sizeof(c) <<endl;//Output 7 cout<<sizeof(d) <<endl;//Output 4 cout<<sizeof(*d) <<endl;//Output cout<<sizeof(**d) <<endl;//Output cout<<sizeof(***d) <<endl;//Output 4 cout<<sizeof(****d) <<endl;//Output 8
Examine the above code and draw the following conclusions:
(1) The data type of a is int[3][5],a[4] and the datatype is int[5],a[0][0] The data type is int. So
sizeof (A) ==sizeof (int[3][5]) ==3*5*sizeof (int) ==60,sizeof (a[4]) ==sizeof (int[5]) =5*sizeof (int) ==20,sizeof (A[0][0 ]) ==sizeof (int) ==4. Although the subscript for A[4] is out of bounds, it does not cause a run-time error because the sizeof operation only cares about the data type and is completed at the compile stage.
(2) because the string ends with a null character ' Char[7 ', the data type of C is ".", so sizeof (c) =sizeof (Char[7]) ==7.
(3) d is a pointer, no matter what data type it points to, the size of itself is always 4, Xixiang. So sizeof (d) ==4. sizeof (*D) has a data type of double*[3][6], so sizeof (*D) ==sizeof (double*[3][6]) ==3*6*sizeof (double*) ==18*4==72. Similarly, sizeof (**D) ==sizeof (double*[6]) ==6*sizeof (double*) ==24,sizeof (***d) ==sizeof (double*) ==4,sizeof (****d) can be inferred sizeof (double) ==8.
When the array is a function parameter, what should the values of I and J below be?
void foo1(char a1[3]){ intsizeof// i == ?}void foo2(char a2[]){ intsizeof// j == ?}
Maybe when you try to answer J's value you realize I was wrong, yes, i!=3. Here the function parameter A1 is no longer an array type, but is transformed into a pointer, equivalent to char* A1, why? It's not hard to think about it. When we call the function foo1, will the program allocate an array of size 3 on the stack? No! An array is a "pass-through", where the caller simply passes the address of the argument, so A1 is naturally the pointer type (char*), and I has a value of 4, and J is also 4.
Mistakes, shortcomings, is inevitable, welcome peer message criticism Correct!
Reference documents
[1] Chen Gang. Advanced Step-by-step tutorials for C + + [M]. Wuhan: Wuhan University Press, 2008.
[2]http://blog.csdn.net/freefalcon/article/details/54839
Full analysis of sizeof (ON)