However, natural spicy chicken --003 sizeof operator and logs of strlen

(The following excerpt from the whole network, intrusion delete apologize)

In the C language, the sizeof () is a data type or expression determines the length of the operator, the processing of the sizeof () is performed at compile time.

Key role in determining the type of data symbols of length
calculation process of the number of bytes in the compiled program when
the number of bytes occupied memory Return Value

sizeof is an operator (operator) C / C ++ is, simply, a number of which function is to return the memory occupied by the object type or bytes.

There are two forms of syntax sizeof follows:
sizeof (TYPE_NAME); // sizeof (type);
sizeof Object; // sizeof objects;

Therefore,
int i;
sizeof (i); // ok
sizeof; // ok
sizeof (int); // ok
sizeof int; // error

sizeof calculated size of the object is calculated translate into object types, that is, different objects of the same type which sizeof values are consistent. sizeof expression is evaluated for a compiler to determine the size according to the final result of the type of expression, the expression is generally not calculated.
strlen calculated specific length of the string, sizeof after calculating the amount of memory occupied by the statement.

strlen work done is a counter, which from a certain location in memory (can be a beginning of the string, in the middle somewhere, and even some unspecified area of ​​memory) start scanning until he came to the end of the first string character '\ 0' so far, and then returns the counter value (length does not include '\ 0').

Header string.h (C) or cstring (C ++)
format strlen (character pointer expression)
function is calculated for a given length of string
returns the given string (including "\ 0") to a length value

strlen (char *) function evaluation is the actual length of the string, it is to be obtained from the beginning to experience the first '\ 0', if you define only did not give it initial value, the result is uncertain, it would have been looking down from aa first address until it encounters '\ 0' stop.
https://m.baidu.com/from=1012852y/s?word=strlen&sa=tb&ts=1109211&t_kt=0&ie=utf-8&rsv_t=cdf8SIs0dqJqpP2fWfzQYCDr6X1MF5uQn6J2XFIMC%252ByGp8kY3aXA4ax6Lx6yyXc&rsv_pq=8442821039947213956&ss=100&tj=1&rqlang=zh&sugid=15108970644672845905&rsv_sug4=4029&inputT=2449&oq=sizeof% 92%%% 8Cstrlen E5 E7. 9A%%% 84% E5 E5% BA% 8C% AB 88%
strlen with sizeof distinction:

calculated specific length of the string strlen (string only), not including the end of the string. It returns the number of characters.

After the calculated amount of memory declared occupied sizeof (byte size), not the actual length.

a byte is taken sizeof operator, and is a function strlen.

Return Value Number of bytes sizeof = number of characters in the character * occupied, the actual length less than the length defined character, in which case the number of characters equal to the defined length. If no size is given definition, discussion classification, for a string array, the character size is equal to the actual number of characters +1; For an integer array, the actual number of characters for the number of characters. String 1 byte per character, integer number of bytes of data per character is the number of bits required to determine the type of system, 32-bit 4 bytes.   

sizeof operator is, as typedef unsigned int in the header file, i.e., the value at compile time calculate the parameter can be an array, a pointer, type, object, functions, and so on. Its functions are: to obtain assurance can accommodate maximum target established by the byte size.

Since computed at compile time, and therefore the size can not be used to return sizeof dynamically allocated memory space. In fact, to the return type and statically allocated with the sizeof the object, structure, or an array of space occupied, with the return value of the object, structure, content stored in the array does not matter.

strlen made only a work counter, which from a certain location in memory (can be a beginning of the string, in the middle somewhere, and even some unspecified area of ​​memory) start scanning, until he came to the first string terminator '\ 0' so far, and then returns the counter value (length does not include '\ 0'). Analyzing the data type in C language expressions or character length; is not a function that computes the number of bytes in the assembly time, and not only computed during program execution.

Guess you like

Origin www.cnblogs.com/jun-ruo-sui-nian/p/diary_003.html