In-depth understanding of sizeof: room Bing Yi
Recently, someone in the forum will have a question about SizeOf, and I have never got a good solution to this problem, and the index today has a more detailed summary of it, and combined with Strlen, if you can help everyone. This is my greatest gratifying. First, first look at the definition of SIZEOF and STRLEN in MSDN: first look at how to define SIZEOF on an MSDN:
SizeOf Operator
SizeOf Expression
The Sizeof Keyword Gives The Amount of Storage, in Bytes, Associated with a variable or a type
(Including Aggregate Types). This Keyword Returns A value of type size_t.
The Expression is Either An Identifier OR A TYPE-CAST Expression (a Type Specifier Enclosed in
ParentheSes.
When Applied to A Structure Type or Variable, Sizeof Returns The Actual Size, Which May Include
Padding bytes inserted for alignment .hen applied to a staticly dimensioned array, sizeof
Returns the size of the entire array. The sizeof operator cannot Return the size of dynamically
Allocated arrays or external arrays.
Then look at how Strlen is defined:
Strlen
Get the length of a string.
Routine Required Header:
Strlen
SIZE_T STRLEN (Const Char * String);
Parameter
String: null-terminated string
Libraries
All Versions of The C Run-Time Libraries.
Return Value
Each of these functions Returns The Numr of Characters in string, Excluding The Terminal
Null. No Return Value is reserved to indeicate an error.
Remarks
Each of these functions returns the number of character of characters in string, not including the
Terminating Null Character. wcslen is a wide-character version of strlen; the argument of
WCSLEN IS A WIDE-Character String. Wcslen and Strlen Behave Identically Otherwise.
Second, several examples say.
First example:
Char * ss = "0123456789";
SIZEOF (SS) result 4 === "SS is a character pointer to a string constant
SizeOf (* SS) Result 1 === "* SS is the first character
CHAR SS [] = "0123456789"; SIZEOF (SS) result 11 === "SS is an array, calculated to / 0, so 10 1
SizeOf (* SS) Result 1 === "* SS is the first character
CHAR SS [100] = "0123456789";
SizeOf (SS) result is 100 === "SS indicates the size of 100 × 1 in memory
Strlen (SS) result is 10 === "Strlen is the internal implementation of a function is before using a loop to / 0.
INT SS [100] = "0123456789";
SIZEOF (SS) result 400 === "SS indicates the size of the reload 100 × 4
Strlen (SS) error === "Strlen parameters can only be char * and must be ended with '' / 0 '' '
Char q [] = "abc";
CHAR P [] = "A / N";
SizeOf (Q), SizeOf (P), Strlen (Q), Strlen (P);
The result is 4 3 3 2 Second Example:
Class X
{
INT I;
Int J;
CHAR K;
}
X x;
Cout <
<
Third example:
Char Szpath [MAX_PATH]
If you define this in the function, SizeOf (szpath) will be max_path, but when Szpath is declared as a virtual ginseng (Void (char szpath [max_path)), sizeof (szpath) will be 4 (pointer size)
Third, SIZEOF understands.
1. The result type of thesizeof operator is size_t, which in the header file is typedef for the unsigned int type. This type ensures that the byte size of the maximum object established can be accommodated.
2.SizeOf is the operator, and strlen is a function.
3.Sizeof can use the type to do parameters, Strlen can only use char * to do parameters, and must be ended with '' / 0 ''. SizeOf can also use functions to do parameters, such as short f ();
Printf ("% d / n", sizeof (f ()));
The result of the output is SizeOf (Short), ie 2.
4. The array does not degenerate the parameters of SizeOf, and it is degraded to the Strlen.
5. Most compilers have calculated SIZEOF when compiling, the length of the variable is the length of the variable. This is why sizeof (x) can be used to define the array of dimensions. CHAR STR [20] = "0123456789";
INT A = Strlen (STR); // a = 10;
INT B = SIZEOF (STR); // and b = 20;
6. The result of Strlen is to calculate when it is running, and the length of the string is used to calculate the length of the string, which is not the size of the memory.
7.SizeOf If the type must be added to the arc, if it is a variable name, it is not incorporated. This is because SizeOf is an operator is not a function.
8. When applies to a structural type or variable, SizeOf returns the actual size when applicable to a static space array, and SIZEOF returns the size of the entire array. The SizeOf operator cannot return the size of the dynamically assigned array or an external array.
9. The array is transmitted as a parameter to the function is a pointer instead of an array, and is the first address of the array, such as Fun (char [8]) Fun (char [])
All equals Fun (char *) pass arrays in C will always pass pointers to the first elements of the argument, the compiler does not know the size of the array if you want to know the size of the array in the function, you need this: Enter the function after entering the function Memcpy copies, the length is passed into Fun (unsiged char * p1, int LEN)
{
Unsigned char * buf = new unsigned char [len 1]
Memcpy (BUF, P1, LEN);
}
See also see: C Primer?
10. Calculating the size of the structural variables must discuss the data alignment problem. For the speed of the CPU (this is related to the CPU number of CPUs, detailed introduction) can refer to some computer principles), C often puts the size of members in the structural variables in the size of the members of the structural variables during processing data. 4 or 8 Calculate, this is called data alignment. Doing so may waste some memory, but the theoretical speed is fast. Of course such settings are inconvenient when reading and writing a data file or exchange data generated by some other applications. Alignment in MS VC , sometimes SizeOf is not equal to actual. Generally, the setting of #pragma pack (n) is added in VC . If you want to store without data alignment, you can modify the Data Alignment in the Advanced Compiler page in the Options dialog box in bytes. Align.
11. SizeOF operator cannot be used for function type, incomplete type, or bit field. Incomplete type refers to a data type with unknown storage size, such as an unknown storage size, an unknown content of the content or a combination type, a Void type, and the like. If SIZEOF (MAX) is defined as int max (), sizeof (char_v) If CHAR_V is defined as char char_v [max] and Max unknown, the SIZEOF (Void) is not correct form four, concluded
SIZEOF use.
1. One of the SizeOF operator is to communicate with routines such as storage allocation and I / O systems. For example: void * malloc (size_t size),
SIZE_T FREAD (Void * PTR, SIZE_T SIZE, SIZE_T NMEMB, FILE * stream).
2. Use it to see a type of object in memory in memory. Void * Memset (Void * S, INT C, SIZEOF (s))
3. When dynamically assigned an object, you can let the system know how much memory you want to assign.
4. Easy to expand some types of expansion, there are many structures in Windows, there is a dedicated field for use to place the byte size of this type.
5. Since the number of bytes of operands may change when implementation, it is recommended to replace constant calculations in a size of the operating number.
6. If the operand is the shape parameter ginseng or function type in the function, SIZEOF gives the size of its pointer.