Programmer interview questions (41): a misunderstanding of the compiler on memory fill Length

Source: Internet
Author: User
People who read "C ++ Object Model" often mistakenly think that the compiler is filled with the length of the computer, as shown below:

Class
{
Double;
Char B;
};

Sizeof (A) =?

People who do not know how to fill in will think it is 9, and those who read the C ++ Object Model (like me) will often think it is 12 《ProgramA similar question in the employee interview book started to think that the answer was wrong .. I did not know after a try today. I was wrong .. The answer to the above question (by default in the compiler) is 16, which is true for vc6.0, mingw, and vs.net ..

In the programmer interview book, the principle of CPU optimization is roughly like this: for n-byte elements (n = 2, 4, 8 ......) The first address can be divisible by N to obtain the best performance. This principle can be followed when designing the compiler. That is to say,By default, the compiler usually takes the maximum variable length as the fill length, instead of the byte length.. Of course, you can also use # pragma pack (n) to specify the padding length of the compiler. This should not be the case with the highest CPU efficiency.

Another netizen discussed whether a class contains another class object and whether it is filled with the length of the containing class? This is not the case,Fill in according to the maximum length of the basic type in the language.Unexpectedly, this bt question will also be taken into the interview questions for a long time.

 

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.