Differences between sizeof and strlen

Source: Internet
Author: User
The strlen (char *) function is used to calculate the actual length of the string. The method is from the beginning to the First '\ 0'. If you only define that the initial value is not assigned to it, the result is not correct. It will be searched from the first address of AA until '\ 0' is stopped.
Char AA [10]; cout <strlen (AA) <Endl; // The result is uncertain.
Char AA [10] = {'\ 0'}; cout <strlen (AA) <Endl; // The result is 0.
Char AA [10] = "Jun"; cout <strlen (AA) <Endl; // The result is 3.
The sizeof () function returns the amount of memory occupied after variable declaration, not the actual length.
Sizeof (AA) returns 10
Int A [10]; sizeof (a) returns 40
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 a parameter using a function, for example:
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 Program During compilation, sizeof is computed as the type or variable length. This is why sizeof (x) can be used to define the array dimension.
Char STR [20] = "0123456789 ";
Int A = strlen (STR); // A = 10;
Int B = sizeof (STR); // B = 20;
6. The strlen result can be calculated only during 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 applicable to a structure type or variable, sizeof returns the actual size,
When it is applicable to static space arrays, 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, the pointer instead of an array is passed, 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 by parameters 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:
After entering the function, copy it with memcpy. 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 usually use sizeof and strlen to calculate the length of the string array.
After reading the detailed explanation above, we can see that there is a difference between the two. From this example, we can see clearly:
Char STR [20] = "0123456789 ";
Int A = strlen (STR); // A = 10; >>>> strlen calculates the length of the string, ending with 0x00 as the string.
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 will be 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
Long Integer, so it is 4
Sizeof (* ss) Result 1 = "* ss is the first character. In fact, it obtains the memory space occupied by the first '0' character string and is a char class.
Type, occupies 1 place
Strlen (SS) = 10 >>> to obtain the length of this string, use strlensizeof to return the size of the byte occupied by the object. // correct
Strlen returns the number of characters. // correct
When sizeof is used, there is a special situation, that is, the array name to pointer transformation,
Char array [3] = {'0 '};
Sizeof (array) = 3;
Char * P = array;
Strlen (p) = 1; // The result of sizeof (P) is 4.
When an array name is passed to a function, it is completely degraded into a pointer.
----------------------------------------------------------
After reading the above, do you know the difference between sizeof and strlen? If you still don't understand it, let's look at the following examples:
Example 1
Char * Ss = "0123456789 ";
Sizeof (SS) Result 4 ===" SS is a character pointer to a String constant
Sizeof (* ss) Result 1 = "* ss is the first character
Most compilation programs calculate sizeof as a type or variable length during compilation.
This is why sizeof (x) can be used to define the array dimension.
Char STR [20] = "0123456789 ";
Int A = strlen (STR); // A = 10;
Int B = sizeof (STR); // B = 20;
Most compilation programs calculate sizeof as a type or variable length during compilation.
This is why sizeof (x) can be used to define the array dimension.
Char STR [20] = "0123456789 ";
Int A = strlen (STR); // A = 10;
Int B = sizeof (STR); // B = 20;
Char ss [] = "0123456789 ";
Sizeof (SS) Result 11 ===" SS is an array, calculated to the \ 0 position, so it is 10 + 1
Sizeof (* ss) Result 1 = "* ss is the first character
Char ss [100] = "0123456789 ";
The result of sizeof (SS) is 100 ===" SS indicates the size in the memory is 100 × 1
The strlen (SS) result is 10 ===" strlen is a function internally implemented by a loop before \ 0.
Int ss [100] = "0123456789 ";
Sizeof (SS) Result 400 ===" SS indicates the size of 100x4 in the memory
Strlen (SS) Error ===" the strlen parameter can only be char * and must end with '\ 0'
Char Q [] = "ABC ";
Char P [] = "A \ n ";
Sizeof (Q), sizeof (P), strlen (Q), strlen (P );
The result is 4 3 3 2.
Example 3
Char szpath [max_path]
If this is defined in the function, sizeof (szpath) will be max_path, but when szpath is declared as a virtual parameter (void fun (char szpath [max_path]), sizeof (szpath) is 4 (pointer size)
Child
Char szpath [max_path]
If this is defined in the function, sizeof (szpath) will be max_path, but when szpath is declared as a virtual parameter (void fun (char szpath [max_path]), sizeof (szpath) is 4 (pointer size)
Another netizen also gave a good explanation:
In fact, to understand sizeof, you only need to grasp one key point: Stack
Program storage is distributed in three areas: Stack, static, and dynamic. From Code Objects that are directly operated, including any type of variables and pointers, are on the stack; dynamic and static storage areas rely on the stack to refer to indirect operations by all needles. The sizeof operator calculates the projection volume of an object on the stack. Remember this.
Char const * static_string = "hello ";
Sizeof (static_string) is a pointer of sizeof, so in 32bit system is 4
Char stack_string [] = "hello ";
Sizeof (stack_string) is an array of sizeof, so it is 6 * sizeof (char) char * string = new char [6];
Strncpy (string, "hello", 6 ");
Sizeof (string) is a pointer of sizeof, so it is still 4. Unlike the first one, this pointer points to a dynamic storage area instead of a static storage area.
No matter where the Pointer Points to, sizeof gets the stack size of the pointer.
The processing of references in C ++ is special. The result of a sizeof reference is the size of a sizeof referenced object.
Struct o
{
Int a, B, c, d, e, f, g, h;
};
Int main ()
{
O & R = * New O;
Cout <sizeof (o) <Endl; // 32
Cout <sizeof r <Endl; // also 32
System ("pause ");
}
R references the entire O object rather than the pointer to O, so the result of sizeof R is exactly the same as that of sizeof O.

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.