The difference and connection between sizeof and strlen

Source: Internet
Author: User

First, sizeof
sizeof (...) is an operator, the typedef in the header file is unsigned int, and its value is 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, 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-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 the Terminator is encountered null. The returned length size does not include NULL.
*****************

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: sizeof returns the size of the array space allocated by the compiler when the ARR array is defined, and does not care how much data is stored in it. Strlen only cares about the contents of the stored data and does not care about 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: The first output 23 actually each run may be different, depending on what is stored in Parr (from parr[0] to know the first null end encountered); the second result is actually intended to calculate the size of the dynamic memory space that the Parr points to, but it backfired, sizeof thinks that Parr is a character pointer, so it returns the space occupied by the pointer (the pointer is stored with a long integer, so it is 4), and the third result, because *parr represents a character stored in the address space referred to by Parr, the length is 1.
************

Iv. References:
The difference and connection between sizeof and strlen (turn)

The result type of the 1.sizeof operator is size_t, which is a typedef of type unsigned int in the header file.
This type guarantees that the byte size of the largest object being built can be accommodated.

2.sizeof is an operator and strlen is a function.

3.sizeof can be used to type parameters, strlen can only use char* to do the parameters, and must be "" "to end.
sizeof can also use functions to make arguments, such as:
Short f ();
printf ("%d\n", sizeof (f ()));
The result of the output is sizeof (short), which is 2.

4. The parameters of the array do sizeof do not degenerate, passed to the strlen is reduced to a pointer.

5. Most compilers have computed sizeof at compile time, which is the length of the type or variable, which is why sizeof (x) can be used to define the number of dimensions of an array.
Char str[20]= "0123456789";
int A=strlen (str); a=10;
int b=sizeof (str); and b=20;

The results of 6.strlen are calculated at run time, and are used to calculate the length of the string, not the size of the memory.

7.sizeof after if the type must be parentheses, if the variable name can be non-parenthesized. This is because sizeof is an operator and not a function.

8. When applied to a struct type or variable, sizeof returns the actual size,
When applied to a static spatial array, sizeof returns the dimensions of all arrays.
The sizeof 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 [])
is equivalent to Fun (char *)
In C + +, 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 inside a function, you need to do this:
After entering the function, copy it with memcpy, 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 often use sizeof and strlen, usually to calculate the length of the string array
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; >>>> Strlen calculates the length of the string to end with the Terminator 0x00 for the string.
int b=sizeof (str); and b=20; >>>> sizeof calculates the size of the memory space allocated by the array str[20], which is not changed by the contents 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) Result 4 = = = SS is a character pointer to a string constant, and sizeof obtains the space occupied by a pointer, which should be

Long integer, so it's 4.
sizeof (*SS) Results 1 = = = "*ss is the first character that actually gets the memory space occupied by the first bit ' 0 ' of the string, which is the Char class

The 1-bit

strlen (ss) = >>>> If you want to get the length of this string, be sure to use strlen

The difference and connection between sizeof and strlen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.