Based on the difference between sizeof and strlen and the use of contact _c language

Source: Internet
Author: User

First, sizeof
sizeof (...) Is the operator, the typedef in the header file is unsigned int, the values are computed at compile time, and the arguments can be arrays, pointers, types, objects, functions, and so on.
Its function is to obtain the size of the byte that is guaranteed to hold the maximum object that the implementation creates.
Because it is evaluated at compile time, sizeof cannot be used to return the size of the dynamically allocated memory space. In fact, the return value has nothing to do with the object, structure, or the contents of the array, by using sizeof to return the type and the space occupied by statically allocated objects, structures, or arrays.
Specifically, when the arguments are as follows, the value returned by the sizeof means the following:
Array-the size of the array space allocated at compile time;
Pointer-The amount of space that is used to store the pointer (the length of the address where the pointer is stored is a long integer and should be 4);
Type-the size of the space occupied by the type;
Object--The actual space size of the object;
Function--the size of the space the return type of the function occupies. The return type of a function cannot be void.
**************

Second, strlen
Strlen (...) is a function that can be computed at run time. parameter must be a character pointer (char*). When an array name is passed in as a parameter, the array is actually degraded to a pointer.
Its function is to return the length of the string. The string may be defined by itself, or it may be random in memory, and the function actually completes is to begin traversing the first address representing the string until the end character is encountered null. The length returned does not include NULL.
*****************

Three, for example:
    eg1, char arr[10] = "What?";
              int len_one = strlen (arr);
              int len_two = sizeof (arr);
              cout << len_one << " and "<< len_two << Endl;
    output: 5 and
    Reviews: sizeof returns the size of the array the compiler assigns to the ARR array when it is defined, and does not care how much data is stored in it. Strlen only cares about the content 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
Comment on: The first output 23 can actually run differently each time, depending on what's in the Parr (starting with parr[0] to know that the first null end was encountered); the second result was actually intended to compute the size of the dynamic memory space that Parr pointed to, but it backfired. sizeof that Parr is a character pointer, so it returns the space occupied by the pointer (the pointer is stored in a long integer, so it is 4), and the third result is that the length is 1 because *parr represents the characters that the address space of the Parr refers to.
************

IV. Reference materials:
The difference and relation between sizeof and strlen (turn)

The result type of the 1.sizeof operator is size_t, which is typedef in the header file as the unsigned int type.
This type guarantees the byte size of the maximum object that the implementation is built to hold.

2.sizeof is the operator, strlen is a function.

3.sizeof can be used to make parameters, strlen can only be used char* parameters, and must be "" "end.
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), that is, 2.

4. The parameters of the array do sizeof are not degraded, passing to the strlen is degraded to the pointer.

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

The results of 6.strlen will be computed at run time to calculate the length of the string, not the type of memory.

7.sizeof after the type must be bracket, if the variable name can be without parentheses. 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 a static space array is applied, the sizeof returns the dimensions of all the arrays.
The sizeof operator cannot return the dimensions of an array or an external array that is dynamically assigned.

9. The array is passed as a parameter to the function newsletters is a pointer rather than 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 within a function, you need to do this:
After entering the function, it is copied with memcpy, and the length is transmitted 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 to compute the length of the string array.
Read the detailed explanation above, found that the use of the two is still different, from this example can be seen very clearly:

Char str[20]= "0123456789";
int A=strlen (str); a=10; >>>> strlen computes the length of the string, ending with the end character 0x00.
int b=sizeof (str); and b=20; >>>> sizeof calculates the size of the allocated array str[20], which is not subject to changes in the contents of the stored memory.

This is the result of processing a static array, and if it's a pointer, the result is different.

char* ss = "0123456789";
sizeof (ss) Result 4 = = "SS is a character pointer to a string constant, sizeof gets the space occupied by a pointer, which should be

Long integer, so it's 4.
sizeof (*SS) result 1 = = *ss is the first character in fact to get the string of the first ' 0 ' occupied by the memory space, is a char class

Type, which accounts for 1 digits.

Strlen (ss) = >>>> If you want to get the length of this string, be sure to use 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.