First, sizeof
sizeof (...) is aOperator, not a function.
A simple example:
int A;
cout<<sizeof a<<endl;
The typedef in the header file is unsigned int, whose value is calculated at compile time, and the parameters can be arrays, pointers, types, objects, functions, and so on.
its functions are:The size of the byte that is guaranteed to accommodate the largest object that is established is achieved .
because it is evaluated at compile time, sizeof cannot be used to return the size of the dynamically allocated memory space.
In fact, using sizeof to return a type and the space occupied by statically allocated objects, structures, or arrays, the return value is not related to what the object, structure, or array stores.
specifically, when the parameters are as follows, the value returned by sizeof represents the following meanings:
array-The size of the array space allocated at compile time;
pointer-The amount of space used to store the pointer (the length of the address where the pointer is stored is a long integer, which should be 4);
type-The amount of space that the type occupies;
Object-The actual amount of space occupied by the object;
function--the amount of space that the return type of the function occupies. The return type of the function cannot be void
Second, strlen
strlen (...) is a function that can be evaluated at run time.
The argument must be a character pointer (char*), and must be terminated with '/'.When an array name is passed in as a parameter, the array is actually degenerate into a pointer.
int ac[10];
cout<<sizeof (AC) <<endl;
Cout<<strlen (AC) <<endl; (AC equals a pointer, but strlen can only accept char* type, so compile-time error)
Its function is: Returns the length of the string. The string may be either self-defined or random in memory, and the function actually completes the function of iterating from the first address representing the string until the Terminator is encountered. The returned length size does not include '/'.
Three. Supplement the difference between strlen and length
both are the lengths of the string, but the parameters of strlen () must be char*, and Str.length () is a member function called by the String class object str, so they are used in different places;
char* ch= "Asdfsafas";
String str= "ADFADF";
Cout<<str.length ();
Cout<<strlen (str); Error
Cout<<strlen (CH);
Cout<<ch.length (); error
The definition of strlen () is basically as follows:
int strlen (const char *STR)//input parameter const
{
ASSERT (STRT! = NULL); Assertion string Address not 0
int Len;
while ((*str++)! = ' + ')
{
len++;
}
return Len;
}
The difference between sizeof and strlen, length