Turn from: http://blog.csdn.net/yushuai007008/article/details/7070589
1. Define: sizeof is an operator (operator) in C/s + +, which simply functions by returning an object or a type's memory byte count. Its return value type is size_t and is defined in the header file stddef.h. This is a value that relies on the compiler system, generally defined as a typedef unsigned int size_t; There are all kinds of compilers in the world, but as a specification, they guarantee that the sizeof value of Char, signed char, and unsigned char is 1, after all, char is the smallest data type we can program.2. Grammar: sizeof has three grammatical forms, as follows: 1) sizeof (object); sizeof (object); 2) sizeof (TYPE_NAME); sizeof (type); 3) sizeof object; sizeof object; So, int i; sizeof (i); OK sizeof I; OK sizeof (int); OK sizeof int; Error since the writing 3 can be replaced by 1, in order to find the form of unity and reduce the burden of our brains, the 3rd way, forget it. In fact, the size of the sizeof compute object is also converted to the computation of the object type, that is, the sizeof value of the same type of object is consistent. Here, the object can be further extended to the expression, that is, sizeof can evaluate an expression, the compiler determines the size based on the end result type of the expression, and generally does not evaluate the expression. such as: sizeof (2); 2 is of type int, so it is equivalent to sizeof (int); sizeof (2 + 3.14 ); The type of 3.14 is double,2 also promoted to double, so it is equivalent to sizeof (double); sizeof can also evaluate a function call, the result of which is the size of the function return type, and the function is not invoked, let's look at a complete example: Char foo () {printf ("foo () has been called.\n"); Return ' a '; int main () {size_t sz = sizeof (foo ());//foo () The return value type is char, so sz = sizeof (char), foo () is not called printf ("sizeof" ( Foo ()) =%d\n ", SZ); The C99 standard stipulates that a function, an expression of an indeterminate type, and a bit-domain (Bit-field) member cannot be computed sizeof values, that is, the following are incorrect: sizeof (foo);//error void Foo2 () {} sizeof ( Foo2 ())//error struct S {unsigned int f1 : 1; unsigned int f2:5; unsigned int f3:12; }; sizeof (S.F1);//Error3. Constants of sizeofThe sizeof calculation occurs at compile time, so it can be used as a constant expression, such as: char ary[sizeof (int) * 10]; OK the latest C99 standard stipulates that sizeof can also be calculated at run time, as the following procedure can be executed correctly in dev-c++: int n; n = 10; n Dynamic assignment char ary[n]; C99 also supports dynamic definition of arrays printf ("%d\n", sizeof (ARY)); Ok. Output 10
But it doesn't work in compilers that do not fully implement the C99 standard, and the code above is not compiled in VC6. So we'd better think sizeof is
The implementation of the translation period, so that will not bring errors, so that the portability of the program more strong.
4. SizeOf of basic data types The basic data types here refer to simple built-in data types such as short, int, long, float, double, and because they are all related to the system, it may be different to take values under different systems. This must be a cause for our attention, and try not to cause trouble to transplant your program in this way.
In general, in a 32-bit compilation environment, the value of sizeof (int) is 4.
5. pointer variable sizeof you should know that the pointer is a very important concept, it records the address of another object. Since it is for the address, it is certainly equal to the width of the address bus select、read inside the computer. So in a 32-bit computer, the return value of a pointer variable must be 4 (note that the result is in bytes), and you can expect the sizeof result of the pointer variable to be 8 in a future 64-bit system. char* PC = "ABC"; int* Pi; string* PS; char** PPC = &pc; void (*PF) ();//function pointer sizeof (PC); The results were 4 sizeof (PI); The result was 4 sizeof (PS); The result was 4 sizeof (PPC); The result is 4 sizeof (PF); The sizeof value of the 4 pointer variable has nothing to do with the object The pointer refers to, because all pointer variables have equal memory size, so the MFC message handler uses two parameters wparam, LPARAM can deliver a variety of complex message structures (using pointers to the struct body).