The difference and connection between sizeof and strlen
First,sizeof
sizeof (...)is an operator, in the header filetypedefto beunsigned int, the values are calculated at compile time, and the parameters can be arrays, pointers, types, objects, functions, and so on.
its function is to ensure that the size of the byte that implements the largest object being built is guaranteed to be accommodated.
because it is evaluated at compile time,sizeofcannot be used to return the size of a dynamically allocated memory space. In fact, withsizeofto return the space occupied by the type and statically allocated objects, structures, or arrays, the return value has nothing to do with what the object, structure, or array stores.
specifically, when the parameters are as follows,sizeofThe value returned represents the following meaning:
Array——the size of the array space allocated at compile time;
Pointers——The amount of space used to store the pointer (the length of the address where the pointer is stored, which is a long integer, which should be4);
type——The size of the space occupied by the type;
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 bevoid.
**************
Second,Strlen
Strlen (...)is a function that can be evaluated at run time. The argument must be a character-type pointer (char*). When an array name is passed in as a parameter, the array is actually degenerate into a pointer.
Its function is: Returns the length of the string. The string may be self-defined or random in memory, and the function actually completes the function of iterating from the first address representing the string until it encounters a terminatorNull. The returned length size does not includeNull.
*****************
Third, examples:
Eg1,Char arr[10] = "What?";
int len_one = strlen (arr);
int len_two = sizeof (arr);
cout << len_one << "and" << len_two << Endl;
the output is:5 and 10
Reviews:sizeofreturn definitionArrarray, the compiler allocates the size of the array space, and does not care how much data is stored in it. Strlenonly cares about the stored data content, not the size and type of space.
Eg2,char * Parr = new CHAR[10];
int len_one = strlen (Parr);
int len_two = sizeof (Parr);
int len_three = sizeof (*parr);
cout << len_one << "and" << len_two << "and" << len_three << Endl;
Output Result:4 and 1
Reviews: First output results23each run may actually be different, depending onParrWhat's in there (fromPARR[0]start to know the first one encounteredNullend); The second result is actually intended to be a calculationParrThe size of the dynamic memory space pointed to, but backfired,sizeofThinkParris a character pointer, so returns the space occupied by the pointer (the pointer is stored with a long integer, so4);A third result, as*parrrepresented byParrrefers to the address space in which the character is stored, so the length is1.
************
Iv. References:
Sizeofwith theStrlenthe difference and contact(turn)
1.sizeofThe result type of the operator issize_t, which is in the header filetypedefto beunsigned inttype.
This type guarantees that the byte size of the largest object being built can be accommodated.
2.sizeofis the operator,Strlenis a function.
3.sizeofyou can use types to make arguments,Strlencan only be usedchar*parameters, and must be in the"" "end of.
sizeofyou can also use functions to make arguments, such as:
Short f ();
printf ("%d\n", sizeof (f ()));
The result of the output issizeof (short), i.e.2.
4.Array to dosizeofthe parameters are not degraded and passed toStrlenit is reduced to a pointer.
5.Most compiled programsat the time of compiling, we putsizeofit's been calculated.is the type or the length of the variable.sizeof (x)reasons that can be used to define the number of array dimensions
Char str[20]= "0123456789";
int A=strlen (str); a=10;
int b=sizeof (str); //andb=20;
6.strlenresults are calculated at run time, and are used to calculate the length of the string, not the size of the memory.
7.sizeofIf the type must be bracketed, the variable name can be non-parenthesized. This is becausesizeofAn operator is not a function.
8.when applied to a struct type or variable,sizeofreturns the actual size,
when applied to a static space array,sizeofreturns the dimensions of all arrays.
sizeofThe operator cannot return the dimensions of an array or an outer array that has been dynamically dispatched
9.The array is passed as a parameter to the function simultaneous is a pointer instead of an array, passing the first address of the array,
such as:
Fun (char [8])
Fun (char [])
are equivalent toFun (char *)
in theC++The parameter passing array is always passed a pointer to the first element of the array, and the compiler does not know the size of the array
If you want to know the size of an array within a function,need to do this:
after entering the function, usememcpycopy it, and the length is passed in by another parameter.
Fun (unsiged char *p1, int len)
{
unsigned char* buf = new unsigned char[len+1]
memcpy (buf, p1, Len);
}
we can use it a lot .sizeofand theStrlen, the length of the string array is usually calculated
read the above detailed explanation, found that the use of the two are still different, from this example can be seen very clearly:
Char str[20]= "0123456789";
int A=strlen (str); a=10; >>>> strlencalculates the length of a string to a terminator0x00ends for the string.
int b=sizeof (str); //andb=20; >>>> sizeofThe allocated array is computedSTR[20]The amount of memory space that is occupied is not changed by the content stored inside it.
above is the result of the static array processing, if the pointer, the result is not the same
char* ss = "0123456789";
sizeof (SS)Results4= = = "Ssis a character pointer to a string constant.sizeofobtained is the space occupied by a pointer,it should be .
long-integer, so it is4
sizeof (*SS)Results1= = = "*ssis the first character is actually getting the first bit of the string' 0 'The amount of memory space that is occupied isCharclass
type of, accounted for1bit
strlen (ss) = Ten >>>>If you want to get the length of this string, be sure to use theStrlen
The difference and connection between sizeof and strlen