Frequently asked questions: the pitfalls and responses of sizeof in C + +

Source: Internet
Author: User

In C + +, sizeof is a frequently asked concept, such as the following several aspects of sizeof's face test repeatedly appeared in the major IT companies in the technical interview, we need to fully understand and grasp.
Note: I was asked this question when I was interviewing a big company.

Interview Question: define an empty data type, there are no member variables and member functions, to the type of sizeof, the result is how much? Answer: is 1, why not 0? An instance of an empty type does not contain any information, and it would have been expected that sizeof should be 0, but when we declare an instance of that type, it must occupy a certain amount of space in memory, otherwise it will not be able to be used (and cannot be asked for sizeof), and as to how much memory is consumed, the compiler decides , an instance of each empty type in Visual Studio occupies 1 bytes of space.   Extension 1: If you add a constructor and destructor to the type and then ask sizeof, what is the result? Answer: It's still 1. Calling constructors and destructors requires only knowing the address of the function, which is related only to the type, and not to the instance of the type , and the compiler does not add any additional information to the instance because of the two functions. Note: This is the result regardless of whether you are adding a constructor or destructor or any other type of function.   Extension 2: What if the destructor is marked as a virtual function ? Answer:the compiler for C + + once found a virtual function in a type, generates a virtual function table for that type, and adds a pointer to the virtual function table in each instance of the type, on a 32-bit machine, a pointer occupies 4 bytes of space, Therefore, sizeof gets 4, or 8if it is 64 bits. test Case:
1#include <stdio.h>2 3 structNulltype {};4 5 structtype16 {7 type1 () {}8~type1 () {}9     intPrint () {printf ("Alexia");return 0; }Ten }; One  A structtype2 - { - type2 () {} the     Virtual~type2 () {} - }; -  - intMain () + { -printf"sizeof (Nulltype) =%d\n",sizeof(Nulltype)); +printf"sizeof (type1) =%d\n",sizeof(type1)); Aprintf"sizeof (type2) =%d\n",sizeof(type2)); at  -     return 0; -}

Note: from "sword Point offer"

Add:

In C , sizeof (' a ') =4 instead of 1 because the character constants in C are of type int , so sizeof (' a ') is sizeof (int), which is different from C + +.

Frequently asked questions: the pitfalls and responses of sizeof in C + +

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.