Differences between sizeof and strlen

Source: Internet
Author: User

Example 1: Char ss [100] = "0123456789 ";

The result of sizeof (SS) is 100. SS indicates the pre-allocated size in the memory, which is 100*1;

The strlent (SS) returns 10. Its internal implementation is to calculate the length of the string in a loop until "\ 0.

Example 2: int ss [100] = "0123456789 ";

The result of sizeof (SS) is 400. SS indicates the memory size, which is 100*4;

Strlen (SS) error. The strlen parameter can only be char * and must end with "\ 0.

Summary differences between sizeof and strlen

The result type of the inclusizeof operator is size_t, and typedef in the header file is of the unsigned int type. This type ensures that it can accommodate the maximum object size.

Inclusizeof is an operator and strlen is a function.

⒊ Sizeof can be a type parameter. strlen can only be a char * parameter and must end with "\ 0. Sizeof can also be used as a parameter using a function, such as: Short F ();

Printf ("% d \ n", sizeof (f ()));

The output result is sizeof (short), that is, 2.

The sizeof parameter in the struct array is not degraded. If it is passed to strlen, It is degraded to a pointer.

Most compile programs calculate sizeof during compilation, which is the type or variable length. This is why sizeof (x) can be used to define the number of digits in an array.

Char STR [20] = "0123456789 ";

Int A = strlen (SATR); // A = 10;

Int B = sizeof (STR); // B = 20;

The result of explain strlen can be calculated only when running. It is used to calculate the length of the string, rather than the memory size occupied by the type.

If it is a type after inclusizeof, brackets must be added. If it is a variable name, no brackets can be added. This is because sizeof is an operator rather than a function.

When a structure type or variable is used, sizeof returns the actual size. When a static space array is used, sizeof returns the size of all arrays. The sizeof operator cannot return the size of the dynamically allocated array or external array.

When a struct array is passed as a parameter to a function, it is passed as a pointer rather than an array, and the first address of the array is passed. Passing an array in C ++ 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 use memcpy in the function to copy the array, and the length is limited by passing the other parameter.

To calculate the size of a structure variable, you must discuss the data issue.

The inclusizeof operator cannot be used for function types, which are incomplete types or bit fields. An incomplete data type refers to a data type with an unknown storage size, such as an array type with an unknown storage size, the structure or union type of the unknown content, and the void type.

 

Sizeof usage

First, we need to make it clear that sizeof is neither a function nor an unary operator. It is a special keyword similar to macro definition, sizeof (). The content in the brackets is not compiled during compilation, but is replaced by an alternative type, such as int A = 8; sizeof (). During the compilation process, no matter what the value of A is, the knowledge is replaced with the type sizeof (INT) and the result is 4. What if sizeof (A = 6? It is also converted to the type, but note that because a = 6 is not compiled, the value of a is 8 after sizeof (A = 6) is executed, is unchanged.

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.