[Pen questions] mistakes in the sizeof series of interview questions

Source: Internet
Author: User

Sizeof series pen questions seem simple. In fact, if you don't go into depth, it is easy to make mistakes, and I am sometimes defeated here. I will give a conclusion in my close-up blog, taking it as a warning.

Differences between V1.0 32-bit and 64-bit Compilers

The test code is as follows:

# Include "stdafx. H "# include <iostream> using namespace STD; int main () {// 32 and 64-bit compilers: Apart from * and long, others remain unchanged (32-bit and 64-bit) // 32: sizeof (*) = 4 sizeof (long) = 4 // 64: sizeof (*) = 8 sizeof (long) = 8 // here the 32-bit compiler cout <"bool size =" <sizeof (bool) <Endl; // 1 cout <"char size =" <sizeof (char) <Endl; // 1 // -- cout <"wchar_t size =" <sizeof (wchar_t) <Endl; // = unsigned short 2 cout <"short Size = "<sizeof (short) <Endl; // 2 short = 2! = Int cout <"int size =" <sizeof (INT) <Endl; // 4 cout <"Long size =" <sizeof (long) <Endl; // 4 [long = Int = 4 is a 32-bit compiler] cout <"long size =" <sizeof (long) <Endl; // 8 cout <"char * =" <sizeof (char *) <"int * =" <sizeof (int *) <Endl; // 4 [* = 4 is a 32-bit compiler] // -- cout <"float size =" <sizeof (float) <Endl; // 4 cout <"double size =" <sizeof (double) <Endl; // 8 cout <"Long double size =" <sizeof (Long Double) <Endl; // 8 [note] Return 0 ;}

The execution result is as follows:

V2.0 sizeof character array

The test code is as follows:

# Include "stdafx. H "# include <iostream> using namespace STD; void printsize (char ainfunc []) {printf (" sizeof (ainfunc) = % lu \ n ", sizeof (ainfunc);} int main () {char STR [5] = {0}; char str1 [] = "hello "; char str2 [5] = {'0'}; char str3 [5] = {'0', '0', '0 '}; // STR is an array containing five elements (spaces ''will be automatically filled later). The array name indicates the address of the first element, so sizeof (STR) represents the memory space occupied by the entire array // It is easy to mistakenly think that the address is passed and the value is 4, but in fact you pass the array name, the array name is not equivalent to the address printf ("sizeof (STR) = % lu \ n", sizeof (STR); // 5 printf ("sizeof (str2) = % lu \ n ", sizeof (STR); // 5 printf (" sizeof (str3) = % lu \ n ", sizeof (str3 )); // 5 // * STR indicates the content of the first element address, that is, the value of the first element printf ("sizeof (* Str) = % lu \ n", sizeof (* Str )); // 1 // It is easy to think of as 5, but "hello" is a string, and the last one is '\ 0' printf ("sizeof (Hello) = % lu \ n ", sizeof ("hello"); // 6 // str1 is an array that stores strings, so the length is 6 printf ("sizeof (str1) = % lu \ n ", sizeof (str1); // 6 // when the array is passed as a parameter, it actually degrades to a normal pointer, but this pointer is actually similar to const pointer printsize (STR ); // 4 return 0 ;}

The execution result is as follows:

V3.0 strlen character array
# Include "stdafx. H "# include <iostream> using namespace STD; int main () {char STR [5] = {0}; char str1 [] =" hello "; char str2 [5] = {'0'}; char str3 [5] = {'0', '0', '0 '}; // note that the result returned by the strlen function does not contain \ 0 // ASCII code: decimal 0-character space ''-Escape Character '\ 0'-Code null, so strlen (STR) = 0 printf (" strlen (STR) = % lu \ n ", strlen (STR); // 0 [note] printf (" strlen (str1) = % lu \ n ", strlen (str1 )); // 5 [note] printf ("strlen (str2) = % lu \ n", strlen (str2); // 1 [note] character '0 ', the corresponding ASCII code is 48 printf ("strlen (str3) = % lu \ n", strlen (str3); // 3 [note] printf ("strlen (Hello) = % lu \ n ", strlen (" hello "); // 5 return 0 ;}

The execution result is as follows:

V4.0 sizeof array pointer/pointer Array
# Include "stdafx. H "# include <iostream> using namespace STD; int main () {// vs is a 32-bit compiler char ** str1 [4]; // STR is a pointer array, the array contains four pointers (**) char * (* str2) [4]; // STR is an array pointer, pointing to the char * array char (** str3) [4] containing four pointers; // STR is an array pointer, point to the char array printf ("strlen (STR) = % lu \ n", sizeof (str1) that contains four char )); // 4*4 = 16 printf ("strlen (str1) = % lu \ n", sizeof (str2); // 4 printf ("strlen (str2) = % lu \ n ", sizeof (str3); // 4 return 0 ;}

The execution result is as follows:

[Pen questions] mistakes in the sizeof series of interview questions

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.