C ++ sizeof () and an interview

Source: Internet
Author: User

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 (). It is not compiled during the internal compilation process of parentheses, instead, it is replaced.

For example, int A = 8; sizeof (a); during compilation, no matter what the value of A is, it is replaced with the type sizeof (INT). The result is 4.

If sizeof (A = 6); it is also converted to the type, but note that a = 6 is not compiled, therefore, after sizeof (A = 6) is executed, the value of A is still 8, which remains unchanged!

Remember the following conclusions:

1. Unsigned only affects the meaning (positive and negative) of the highest bit, and the data length will not be changed. So sizeof (unsigned INT) = sizeof (INT );
2. the sizeof value of the custom type is equivalent to its type prototype. For example, typedef short word; sizeof (short) = sizeof (word ).
3. When sizeof is used for a function, it will be replaced by the type of the function return value in the compilation phase. For example:

Int F1 () {return 0 ;};
Cout <sizeof (F1 () <Endl; // the return value of F1 () is int, so it is considered as int
4. As long as it is a pointer, the size is 4. For example, cout <sizeof (string *) <Endl; // 4
5. the array size is the product of each dimension * the size of the type of the array element. For example:

Char A [] = "abcdef ";
Int B [20] = {3, 4 };
Char C [2] [3] = {"AA", "BB "};
Cout <sizeof (a) <Endl; // 8
Cout <sizeof (B) <Endl; // 20*4
Cout <sizeof (c) <Endl; // 6

The size of array A is not specified when it is defined. The space allocated to it during compilation is determined by the initialization value, that is, 8, including '\ 0' not displayed at the end '.
6. Differences between string sizeof and strlen (see the following example ).

Example:
/*************************************** * ******************* Description: the siseof operator is used to return the byte length of an object or type name. It has three forms: * sizeof (type name); * sizeof (object); * sizeof object; ** Author: Charley * datetime: * compile environment: win7 32-bit + vs2008 *********************************** * ***********************/# include <iostream> # include <string> # include <cstddef> using namespace STD; int main_tes T5 () {size_t IA; // The type of the returned value is size_t. This is a machine-related typedef definition. In the header file of cstddef, IA = sizeof (IA ); // OK ia = sizeof IA; // OK // IA = sizeof int; // error IA = sizeof (INT ); // OK int * Pi = new int [12]; cout <"Pi:" <sizeof (PI) // 4 Bytes: pointer size <"* Pi: "<sizeof (* PI) // four bytes: the first element of the array is int type <Endl; // The size of a string has nothing to do with the length of the string it refers to. String ST1 ("foobar"); string st2 ("a mighty oak"); string * PS = & ST1; cout <<"ST1:" <sizeof (ST1) // 32 bytes <"st2:" <sizeof (st2) // 32 bytes <"PS: "<sizeof (PS) // 4 Bytes: pointer size <" * PS: "<sizeof (* PS) // 32 bytes, * PS indicates the string content, which is equivalent to sizeof (string) <Endl; cout <"short: \ t" <sizeof (short) <Endl; // two bytes of cout <"short *: \ t" <sizeof (short *) <Endl; // four bytes of cout <"Short &: \ t "<sizeof (short &) <Endl; // two bytes of cout <" short [3]: \ t "<sizeof (short [3]) <Endl; // 6 Characters Section = size of each element 2 * Three cout elements <"short: \ t" <sizeof (INT) <Endl; // four bytes of cout <"short *: \ t" <sizeof (unsigned INT) <Endl; // four bytes of char a [] = "abcdef "; int B [20] = {3, 4}; char C [2] [3] = {"A", "B"}; cout <sizeof () <Endl; // 8 = 8*1 (char size: 1 byte ), the 8 characters include an empty character ''and hidden '\ 0' cout <sizeof (B) <Endl; // 80 = 20*4 int size is 4 bytes cout <sizeof (c) <Endl; // 6 = 2*3*1 char size is 1 byte // difference between string sizeof and strlen char AA [] = "Abcdef"; // The end is a space char BB [20] = "abcdef"; // The end is a space string S = "abcdef "; // The end is a space cout <strlen (AA) <Endl; // 7, string length, including an empty character '', excluding the hidden '\ 0' cout <sizeof (AA) <Endl; // 8 = 8*1 (char size is 1 byte ), the 8 characters include an empty character ''and hidden '\ 0' cout <strlen (bb) <Endl; // 7, String Length, contains an empty character '', excluding the hidden '\ 0' cout <sizeof (bb) <Endl; // 20 = 20*1, string capacity cout <sizeof (s) <Endl; // 32, which does not represent the length of the string, but the size of the string class // error: parameter 1 cannot be taken from "STD: S Tring "is converted to" const char * "// cout <strlen (s) <Endl; // error! S is not a character pointer. AA [1] = '\ 0'; cout <strlen (AA) <Endl; // 7 cout <sizeof (AA) <Endl; // 8, sizeof is a constant return 0 ;}
One interview question:

The question is to require output: trendmicrosoftuscn, and then require modificationProgramSo that the program can output the above results.CodeAs follows:

 
# Include <iostream> # include <string> using namespace STD; int main (INT argc, char * argv []) {string strarr1 [] = {"trend ", "micro", "soft"}; string * P = new string [2]; P [0] = "us"; P [1] = "cn "; cout <sizeof (strarr1) <Endl; cout <sizeof (p) <Endl; cout <sizeof (string) <Endl; For (INT I = 0; I <sizeof (strarr1)/sizeof (string); I ++) cout <strarr1 [I]; for (I = 0; I <sizeof (P) /sizeof (string); I ++) cout <p [I]; cout <Endl ;}

Answer:

If

For (I = 0; I <sizeof (P)/sizeof (string); I ++)

Change

For (I = 0; I <sizeof (p) * 2/sizeof (string); I ++)

The answer is also incorrect. sizeof (P) is only the pointer size of 4. To find the number of members that array P points to the array, it should be

For (I = 0; I <sizeof (* P) * 2/sizeof (string); I ++)

Why? When the pointer P points to the array, * P points to the members in the array. What is the member type? string type, OK, so sizeof (* P) is 32, multiply by 2 is the size of the entire array.

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.