Differences between sizeof and strlen

Source: Internet
Author: User
[Original]

Char STR [20] = "0123456789 ";

Int A = strlen (STR);/* A = 10; strlen calculates the length of the string, ending with \ 0. Int B = sizeof (STR);/* B = 20; sizeof calculates the memory space occupied by the allocated array STR [20, it is not affected by the content stored in it ================================================ ========================================================== ======================== char * str1 = "absde "; char str2 [] = "absde"; char str3 [8] = {'A',}; char ss [] = "0123456789"; output: sizeof (str1) = 4 sizeof (str2) = 6; sizeof (str3) = 8; sizeof (SS) = 11 first note that the char type occupies one byte, so the sizeof (char) is 1, to understand this, str1 is a pointer, but it only points to the string "absde. Therefore, sizeof (str1) is not the space occupied by strings, nor the space occupied by character arrays, but the space occupied by a character pointer. Therefore, sizeof (str1) = sizeof (char *) = 4. in C/C ++, a pointer occupies 4 bytes. str2 is a struct array. C/C ++ specifies that for an array, the total space occupied by this array is returned. Therefore, sizeof (str2) obtains the total space occupied by the string "absde. In "absde", there are a B S D E \ 0 six characters in total, so the str2 array length is 6, so sizeof (str2) = 6 * sizeof (char) = 6str3 has been defined as an array with a length of 8, so sizeof (str3) is similar to 8str4 and str2, '0' '1 '... '9' and '\ 0' are 11 characters in total, so SS occupies 8 space. For pointers, the sizeof operator returns the space occupied by the pointer, which is generally 4 bytes; for an array, sizeof returns the total space occupied by all elements in the array. Char * and char [] are easy to confuse and must be distinguished. In addition, char * = "AAA" is not recommended and should be avoided, while strlen does not distinguish array or pointer, returns the length until \ 0. In addition, strlen does not include \ 0 in the length of the string. ========================================================== ========================================================== ============== 1. sizeof (...) yes. In the header file, typedef is an unsigned Int. Its value is calculated after compilation. parameters can be arrays, pointers, types, objects, and functions. Its function is to obtain the maximum object size that can be accommodated. Because it is calculated during compilation, sizeof cannot be used to return the size of the dynamically allocated memory space. In fact, sizeof is used to return the type and space occupied by objects, structures, or arrays statically allocated. The returned value has nothing to do with the content stored by objects, structures, and arrays. Specifically, when the parameters are as follows, the values returned by sizeof indicate the following: array -- size of the array space allocated during compilation; pointer-size of the space used to store the pointer (the address length of the pointer, which is a long integer and should be 4); Type-size of the space occupied by the pointer; object: the actual space occupied by the object; function: the space occupied by the return type of the function. The return type of the function cannot be void. * ************* 2. strlen (...) is a function that can only be computed at runtime. The parameter must be a character pointer (char *). When the array name is passed in as a parameter, the array actually degrades to a pointer. Its function is to return the length of a string. The string may be defined by itself or randomly stored in the memory. The function is actually used to traverse from the first address representing the string until the end character is null. The returned length does not include null. * **************** 3. Example: eg1 and char arr [10] = "what? "; Int len_one = strlen (ARR); int len_two = sizeof (ARR); cout <len_one <" and "<len_two <Endl; the output result is: 5 and 10 comment: sizeof returns the size of the array space allocated by the compiler to define the ARR array, regardless of how much data is stored in it. Strlen only cares about the stored data content and does not care about the size and type of the 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: 23 and 4 and 1 comments: the first output result 23 may be different each time it is run, depending on what is stored in Parr (from Parr [0] to know that the first null is ended ); the second result is actually intended to calculate the size of the dynamic memory space that Parr points to, but it is counterproductive. sizeof considers Parr as a character pointer, therefore, the returned result is the space occupied by the pointer (the pointer is stored in a long integer, so it is 4). The third result is, * Parr represents the characters in the address space indicated by Parr. So the length is 1.*4. References: Differences and relationships between sizeof and strlen. 1. the result type of the sizeof operator is size_t. In the header file, typedef is of the unsigned int type. This type ensures that it can accommodate the maximum object size. 2. sizeof is an operator and strlen is a function. 3. sizeof can be a type parameter. strlen can only be a char * parameter and must end with ''\ 0. Sizeof can also be used as parameters using functions, such as: Short F (); printf ("% d \ n", sizeof (f ())); the output result is sizeof (short), that is, 2. 4. the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer. 5. most compilation programs calculate sizeof as a type or variable length during compilation. This is sizeof (X) can be used to define the reason for array dimension char STR [20] = "0123456789"; int A = strlen (STR); // A = 10; int B = sizeof (STR ); // while B = 20; 6. the strlen result can be calculated only when it is running. It is used to calculate the string length, not the memory size occupied by the type. 7. If sizeof is a type, you must add an arc. If it is a variable name, you can do not add an arc. This is because sizeof is an operator and not a function. 8. When a structure type or variable is applied, sizeof returns the actual size. When a static space array is applied, sizeof returns the size of all arrays. The sizeof operator cannot return the size of the dynamically assigned array or external array 9. when an array is passed as a parameter to a function, it is a pointer rather than an array, and the first address of the array is passed, for example, fun (char [8]) Fun (char []) it is equivalent to fun (char *). In C ++, passing an array is always a pointer to the first element of the array, the compiler does not know the size of the array. If you want to know the size of the array in the function, you need to do this: Enter the function and copy it with memcpy, the length is passed into fun (unsiged char * P1, int Len) {unsigned char * Buf = new unsigned char [Len + 1] memcpy (BUF, P1, Len );} when sizeof and strlen are often used, we usually calculate the length of the string array. After reading the detailed explanation above, we find that there is a difference between the two, from this example, we can see that char STR [20] = "0123456789"; int A = strlen (STR); // A = 10; >>>> strlen calculates the length of the string and ends with the string ending with 0x00. Int B = sizeof (STR); // while B = 20; >>> sizeof calculates the memory space occupied by the allocated array STR [20, the stored content is not changed. The above is the result of processing the static array. If it is a pointer, the result is different: char * Ss = "0123456789"; sizeof (SS) result 4: SS is a character pointer to a String constant. sizeof obtains the space occupied by a pointer, which should be a long integer, therefore, it is 4 sizeof (* ss) Result 1 = "* ss is the first character. In fact, it is the memory space occupied by the first '0' of the string, which is of the char type, strlen (SS) = 10 >>> if you want to obtain the length of this string, use strlen ================================================== ========================================================== --------------------- 21 ASPnet Source: csdn Original article: 1539951 Copyright Disclaimer: This article is the author's original article. For more information, see the blog post link!

Differences 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.