1. define
sizeof is an operator (operator).
The function is to return the number of bytes of memory that an object or type occupies.
The return value type is size_t. (size_t is defined in the header file Stddef.h, which relies on the value of the compiled system, generally defined as a typedef unsigned int size_t;)
2. Grammar
There are three grammatical forms of sizeof:
1) sizeof (object); sizeof (object)
2) sizeof object; sizeof objects
3) sizeof (TYPE_NAME); sizeof (type)
An object can be a variable of various types, as well as an expression (general sizeof does not evaluate an expression).
sizeof the object's memory size, and ultimately it is converted to evaluate the object's data type.
sizeof (expression); The size of the data type for which the value is the final result of an expression
Example: (under 32-bit machine)
int i;
sizeof (int); Value is 4
sizeof (i);//value 4, equivalent to sizeof (int)
sizeof I//value 4
sizeof (2);//value 4, equivalent to sizeof (int), because 2 is of type int
sizeof (2 + 3.14); A value of 8, equivalent to sizeof (double), because the result of this expression is of type double
char ary[sizeof (int) * 10]; OK, compile correctly
The latest C99 standard stipulates that sizeof can also be calculated at run time.
The following program can be executed correctly in dev-c++:
int n;
n = 10; n Dynamic Assignment
char ary[n];//C99 also supports dynamic definition of arrays
cout<<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 it is best to think that sizeof is executed at compile time, so that it will not bring error, so that the portability of the program is stronger.
1. SizeOf of basic data types
The basic data types here refer to simple built-in data types such as short, int, long, float, and double.
Because their memory size is related to the system, it may be different to take values under different systems.
2. SizeOf of the structural body
The sizeof of the structure involves the problem of byte alignment.
Why byte alignment is required. The principle of computer composition teaches us that this will help to speed up the number of computers, otherwise it will take more instruction cycles. To do this, the compiler handles the structure by default (as is the case with data variables elsewhere), let the base data type of width 2 (short, etc.) reside on an address that is divisible by 2, so that the base data type (int, etc.) with width 4 is located on an address that is divisible by 4, and so on. In this way, you may need to add padding bytes in the middle of two digits, so the sizeof value of the entire structure increases.
The details of byte alignment are related to the implementation of the compiler, but generally, 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 structure is an integer multiple of the member size relative to the first address of the structure body (offset), and the compiler adds padding bytes (internal adding) between the members if necessary.
3 The total size of the structure is an integral multiple of the size of the widest base type member, and, if necessary, the compiler adds the padding byte (trailing padding) to the last member.
Note: The sizeof value of the empty structure body (excluding data members) is 1. Imagine a "space-less" variable to be addressed, how can two different "empty structure" variables be differentiated, so that the "empty structure" variable is stored so that the compiler can allocate only one byte of space for the placeholder.
Example:
struct S1
{
char A;
int b;
};
sizeof (S1); The value is 8, the byte is aligned, and 3 bytes are populated after char.
struct S2
{
int b;
char A;
};
sizeof (S2); The value is 8, the byte is aligned, and 3 bytes are populated after char.
struct S3
{
};
sizeof (S3); The value is 1, and the empty structure also occupies memory.
3. SizeOf of the Consortium
The structure body is listed sequentially in the memory organization, the consortium is the overlapping type, each member shares a memory, so the sizeof of the whole consortium is the maximum value of each member sizeof.
Example:
Union u
{
int A;
float B;
Double C;
char D;
};
sizeof (U); Value is 8
4. SizeOf of array
The sizeof value of the array equals the number of bytes of memory occupied by the array.
Note: 1 When a character array represents a string, its sizeof value is computed in '/0 '.
2 when the array is a formal parameter, its sizeof value corresponds to the sizeof value of the pointer.
Example 1:
Char a[10];
Char n[] = "abc";
cout<< "Char a[10] " <<sizeof (a) <<endl;//array with a value of
cout<< "char n[] =/" abc/" < <sizeof (n) <<endl;//An array of strings, counting '/0 ', with a value of 4
Example 2:
void func (char a[3])
{
int c = sizeof (a);//c = 4, because here A is not an array type, but a pointer, equivalent to char *a.
}
void FuncN (char b[])
{
int cN = sizeof (b);//cn = 4, Reason Ibid.
}
5. SizeOf of the pointer
Pointers are used to record the address of another object, so the size of the pointer's memory is, of course, equal to the width of the address bus select、read inside the computer.
In a 32-bit computer, the return value of a pointer variable must be 4.
The sizeof value of a pointer variable has nothing to do with the object that the pointer refers to.
Example:
Char *b = "HelloWorld";
Char *c[10];
Double *d;
int **e;
void (*PF) ();
cout<< "char *b =/" helloworld/" <<sizeof (b) <<endl;//pointer to string, value 4
cout<<" Char *b "<<sizeof (*b) <<endl;//Pointer to character, value 1
cout<<" double *d "<<sizeof (d) << endl;//pointer, value 4
cout<< "Double *d " <<sizeof (*d) <<endl;//pointer to floating-point number, value 8
cout<< " int **e "<<sizeof (e) <<endl;//pointer to pointer, value 4
cout<<" Char *c[10] "<<sizeof (c) <<endl;//pointer array with a value of
cout<< "Void (*PF) (); " <<sizeof (PF) <<endl;//function pointer, value 4
6. sizeof of function
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 invoked.
The form of evaluation of functions: sizeof (function name (actual parameter list))
Note: 1 You cannot evaluate a function that has a return value type that is empty.
2 The function name can not be evaluated.
3 The function with parameters, when using sizeof, must write the argument table.
Example:
#include <iostream>
using namespace std;
float FUNCP (int A, float b)
{return
a + b;
}
int FUNCNP ()
{return
3;
}
void Func ()
{
}
int main ()
{
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, with a value of 4,sizeof (FUNCNP ()) equivalent to sizeof (int)
/*cout<<sizeof (Func ()) <<endl;//error,sizeof cannot evaluate a function with a return value of an empty type * *
/*cout< <sizeof (FUNCNP) <<endl; Error,sizeof cannot evaluate function name
return 0;
}