sizeof and Strlen

Source: Internet
Author: User

1. sizeof

1.1 sizeof is a stand-alone operator, not a function. sizeof gives us the size of the memory allocated for the data item. For example:

12 cout << sizeof(long) << endl;   // 输出: 4cout << sizeof(double) << endl; // 输出:8

1.2 If you apply sizeof to a type, you must use parentheses as shown above. But if you use it for a variable, you can do it without parentheses.

12 intx;cout << sizeofx << endl;       // 输出: 4

The 1.3 char type represents a single character, occupying only one byte of memory space, so sizeof (char) =sizeof (' a ') = 1. In addition, the related data item is empty, so sizeof (") is not zero, but an error. However, "" for a C-style string, the end is automatically appended with the Terminator ' \ n ', so there is sizeof ("") =1,sizeof ("") = 2.

123456 char c = ‘c‘;cout << sizeof(char) << endl;   // 输出:1cout << sizeof(c) << endl;      // 输出:1//cout << sizeof(‘‘) << endl;   // 编译时错误:不能为空字符常量cout << sizeof("") << endl;     // 输出:1cout << sizeof("\0") << endl;   // 输出:2

1.4 str1 is a pointer, just pointing to the string "Hello". So sizeof (STR1) is not a space occupied by a string, nor is it a space for a character array, but a space for a character-type pointer. So sizeof (STR1) =sizeof (char*) = 4, and a pointer occupies 4 bytes in C + +. *str and Str[0], all represent the first character in a string ' h ', so sizeof (*STR1) = 1. * (Str1+1) represents the second character, and so on.

1234 const char * str1 = "Hello" cout << sizeof (str1) << endl;   //output: 4 cout << sizeof ( char *) << endl;  //output: 4 cout << sizeof (*STR1) << endl;  //output: 1

1.5 str2 is an array of character types. C + + Specifies that for an array, the total space occupied by this array is returned, so sizeof (STR2) obtains the total space occupied by the string "Hello". In "Hello", there is a total of H e l l o \ 06 characters, so the length of the str2 array is 6, so sizeof (STR2) =6*sizeof (char) = 6.

12 charstr2[]="hello"; cout << sizeof(str2) << endl;   // 输出:6

1.6 STR3 has been defined as an array of length 8, so sizeof (STR3) =8*sizeof (char) = 8.

12 charstr3[8]={‘h‘,}; cout << sizeof(str3) << endl;   // 输出:8

1.7 If the given data item is not a character array, but rather a custom type pair like an array, then sizeof (STR3) =8*sizeof (for image type or pair image). Such as:

12345678 class a {      int X; &NBSP;&NBSP;&NBSP;&NBSP; double D; a a[10]; cout << sizeof (A) << endl;      // Output: 16, memory is padded here, so it's not a cout << sizeof (a) << endl;      //output:

1.8 Most compilers have calculated sizeof at compile time, which is why sizeof (x) can be used to define the length of the array. The length of the array A2 below is determined by the length of the A1.

1234 int a1[10];cout << sizeof(a1)/sizeof(int) << endl;     // 输出:10int a2[sizeof(a1)/sizeof(int)];cout << sizeof(a2)/sizeof(int) << endl;     // 输出:10

The 1.9 sizeof operator cannot return the dimensions of an array or an array that is dynamically assigned, especially when sizeof is applied to an array in the form of a virtual parameter, resulting in a 4 (pointer size). Keep in mind that arrays are always passed the address of an array when passed as a parameter.

1234567891011121314 #define MAXSIZE 100// 此处等同于 void PrintSize(char* str)void PrintSize(char str[MAXSIZE]){    cout << sizeof(str) << endl;        // 输出:4}int main(){    char str1[MAXSIZE];    cout << sizeof(str1) << endl;       // 输出:100    PrintSize(str1);}

2.0 sizeof can also be applied to functions, note that function fun only declares undefined:

1234567 doublefun(); intmain(){    cout << sizeof(fun()) << endl;      // 输出:8    //cout << sizeof(fun) << endl;      // 编译时出错:非法的sizeof操作数}

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

12 //cout << sizeof(void) << endl;     // 编译时出错:非法的sizeof操作数cout << sizeof(void*) << endl;      // 输出:4,就是指针的存储空间

In summary, for pointers, the sizeof operator returns the space that the pointer occupies, typically 4 bytes, and for an array, sizeof returns the total space occupied by all elements of this array. char* and char[] easy to confuse, must distinguish. sizeof can be applied to any built-in type and custom type.

2. strlen

2.1 Strlen is a function that does not distinguish between an array or a pointer, and returns the length of the study until it is reached. And strlen is not to count/0 into the length of the string. The packet header file String.h is required for use.

1234567 char str4[] = "hello";cout << strlen(str4) << endl;   // 输出: 5const char *str5 = "hello";cout << strlen(str5) << endl;   // 输出:5cout << strlen("") << endl;     // 输出:0cout << strlen("\0") << endl;   // 输出:0

2.2 strlen can only be used for C-type strings, note that it must be a string and end with ' + ', which is an error for any other type.

12345678910111213 //cout << strlen(int) << endl;  // 出错:不能用于类型//cout << strlen(‘‘) << endl;   // 出错:不能用于字符//cout << strlen(‘a‘) << endl;  // 出错:不能用于字符char str6[5] = {‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘};//cout << strlen(str6) << endl; // 运行时出错:str6不是一个"C风格字符串",即不是以‘\0‘                                    // 结尾的字符串,此处输出一个不确定值,本例为19class B{    int x;    double d;};B b[10];//cout << strlen(b) << endl;    // 出错:一句话,只能用于字符串,还必须是“C风格的”

The results of 2.3 strlen are calculated at run time.

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.