Sizeof & amp; strlen difference, sizeof Function

Source: Internet
Author: User

Sizeof & strlen difference, sizeof Function

This article mainly records the differences between the sizeof operator and strlen () function, and their respective uses. (If you find any mistakes, leave a message to correct them)

Differences between sizeof and strlen

The sample code is as follows:

# Include <stdio. h> int main () {char str [20] = "0123456789"; printf ("strlen (str) = % d \ n", strlen (str )); // calculate the string length (excluding 0x00). Result: 10 printf ("sizeof (str) = % d \ n", sizeof (str )); // The size is not degraded. Result: 20 char * ss = "0123456789"; printf ("strlen (ss) = % d \ n", strlen (ss )); // result: 10 printf ("sizeof (ss) = % d \ n", sizeof (ss); // calculate the memory space occupied by the ss pointer. Result: 4 return 0 ;}
What are the functions of sizeof?

Try replacing strlen with sizeof to calculate the length of the string

Such replacement is rarely used. Writing like this is only convenient for knowledge points.

// Question: Calculate the length of the string and convert it to uppercase. # Include <iostream> # include <string> using namespace std; void upperCase (char str []) {int test = sizeof (str); // strlen (str) int test2 = sizeof (str [0]); for (size_t I = 0; I <test/test2; ++ I) // I <test {if ('A' <= str [I] & str [I] <= 'Z ') str [I]-= ('A'-'A') ;}} int main () {char str [] = "aBcDe "; // We know from the above that sizeof (array) does not degrade, and the returned result is the size of the entire array, rather than the number of elements in it. // If the array is initialized as char str [9] = "aBcDe", the result is incorrect. Cout <"The length of str is:" <sizeof (str)/sizeof (str [0]) <endl; // strlen (str) upperCase (str ); cout <str <endl; return 0 ;}
Sizeof calculates the size of a union

Sample Code 1:

# Include <iostream> using namespace std; union u {double a; int B ;}; union u2 {char a [13]; int B ;}; union u3 {char a [13]; char B ;}; int main () {cout <"sizeof (u) =" <sizeof (u) <endl; // result: 8 cout <"sizeof (u2) =" <sizeof (u2) <endl; // result: 16 (byte alignment used) cout <"sizeof (u3) =" <sizeof (u3) <endl; // result: 13 (byte alignment used) return 0 ;}

The size of union depends on the Member that occupies the largest space (considering the byte alignment), and the data types that conform to the byte alignment are: union, class, struct.

For CPU alignment problems, the compiler tries its best to put data on its alignment to increase the CPU hit rate. You can use # pragma pack (x) to modify the alignment of the compiler.

The alignment of the C ++ inherent type is a smaller part of the compiler alignment. For example, if the compiler uses 2 alignment and the int type is 4, the int alignment is the smallest of 2 and 4. because the default alignment is 8 (long double), the inherent alignment type is itself. If the values are consistent with the type, if the values are smaller than 8, the maximum alignment of the member type is obtained.

Sample Code 2:

# Include <iostream> using namespace std; # pragma pack (2) // manually change the alignment to 2, so the alignment of int Is also 2. So result 10: If this sentence is removed, the value is 12 union u {char buf [9]; int a ;}; int main () {cout <"sizeof (u) = "<sizeof (u) <endl; return 0 ;}

Related Article

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.