Differences between sizeof and strlen

Source: Internet
Author: User

I. sizeof
Sizeof (...) isOperatorInstead of a function.
The result type of the sizeof operator is size_t. In the header file, typedef is unsigned int, and its value isCompile timeThe calculation is complete. The parameter can beArray, pointer, type, object, Function.
Its functions are as follows:To ensure the maximum size of the object to be created..
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 are as follows:
Array-- Size of array space allocated during compilation;
Pointer-- Size of the space used to store the pointer address (the address for storing the pointer is a long integer and should be 4 );
Type-- The space occupied by this type;
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.

Example:

Char * str1 = "absde ";
Char str2 [] = "absde ";
Char str3 [8] = {'A ',};

Output:

Sizeof (str1) = 4
Sizeof (str2) = 6;
Sizeof (str3) = 8;

First of all, the char type occupies one byte, so sizeof (char) is 1.

Str1 is a pointer that only points to the string "absde. Therefore, sizeof (str1) does not mean that the space occupied by the string is not 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) = 6

Str3 has been defined as an array with a length of 8, so sizeof (str3) is 8

In short, for pointers, the sizeof operator returns the space occupied by this pointer, which is generally 4 bytes. For an array, sizeof returns the total space occupied by all elements of this array. Char * and char [] are easy to confuse and must be distinguished. In addition, char * = "AAA" is not recommended and should be avoided.Strlen does not distinguish whether it is an array or a pointer. It returns the length until \ 0. In addition, strlen does not include \ 0 in the length of the string.




Ii. strlen
Strlen (...) is a function.RuntimeCan be calculated.
The parameter must be a character pointer (char *) and end with '\ 0.When the array name is passed in as a parameter, the array actually degrades to a pointer.
Its functions are as follows:Returns 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 '\ 0' is encountered '. The returned length does not include '\ 0 '.

 

Refer:

Difference between sizeof and strlen http://blog.csdn.net/21aspnet/article/details/1539951

 

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.