C/c ++ interview Summary (1) Interview Summary

Source: Internet
Author: User

C/c ++ interview Summary (1) Interview Summary

I have recently been looking for a new job and have encountered many interview questions. Most of them have made me very embarrassed. Once again, I realized that my knowledge is lacking. my previous job was as a fresh graduate, at all times, the project team did not have many requirements. after entering the project team, I was still studious (I thought). I had no relevant experience and I was very confident (in fact, they were all blind ), in my previous job, I met several old employees who were willing to bring me a lot of help (and indirectly caused me to be arrogant). In my previous job, I used C ++, I often use STL (I think I am familiar with it. The interviewer asked me what is the difference between MAP and LIST... Face-to-Face !!), Fortunately, I was lucky enough to find a job. The following is my summary of the interview questions:

1. The Byte alignment problem (almost every company has interview questions). Below are some concepts of byte alignment that I copied from the Internet.

First, an important concept is: why do we need byte alignment? (I don't know this before, but sometimes I don't know why it is)

(1) the root cause of byte alignment is the efficiency of CPU data access. For 32-bit machines, 4-byte alignment can improve the access efficiency, if the double type is 8-byte and exceeds the 4-byte boundary storage, the cpu will read twice and the efficiency will be reduced. In vc, the default value is 4-byte alignment, GNU gcc is also the default 4-byte alignment.

Before solving these questions, you need to know what the byte alignment principle is (you can Baidu bit byte alignment)

(1) The first address of the struct variable can be divisible by the size of its widest basic type member.

(2) The offset of each member of the struct to the first address of the struct is an integer multiple of the member size (if not, the compiler will fill in bytes between the Members to align them ).

(3) the total size of a struct is an integer multiple of the size of the widest basic type of the struct. (If not, the compiler will fill in bytes after the last member ).

Struct Test

{

Char t1;

Double t2; // The offset must be an integer multiple of itself. sizeof (double) is 8. Therefore, according to the (2) criterion, seven bytes must be filled between t1 and t2.

Int t3; // The offset of t3 is 16 bytes, and sizeof (int) is, which is a multiple of it. The total number of bytes is 1 + 7 + 8 + 4 = 20, according to the (3) criterion, the value must be a multiple of 8. Four bytes must be entered after all T3.

};

In summary, sizeof (Test) = 24;

What if it is a union? Let's look at the following example.

Typedef union

{

Char;

Int B [5];

Double c;

};

We know that the size of the Union is the size of its widest basic type member. Here it is obvious that the widest array is 20 bytes. is the size of the Union 20? Obviously not, because the size of the Union should be a multiple of the size of all basic types in addition to the widest basic type, and all must be filled with four bytes, therefore, the size of the Union is 24.

The association can also be used as a member of the struct. You can search for these questions on the Internet.

Http://bbs.csdn.net/topics/360183704 this blog you can also take a look at your interview is certainly helpful.


2. What is the difference between static global variables and normal global variables? What is the difference between static local variables and common local variables? What is the difference between a static function and a common function?

(1) Their difference lies in the different scopes. The scope of static global variables is limited to the file that declares them, global variables without static modification can also be used in other files through extern. They are stored in static mode,

(2) static local variables are initialized only once. The value used next time depends on the previous value. static local variables are destroyed at the end of the program, local variables will be released after their scope ends.

Void fun ()

{

Int I = 0;

Static int j = 1;

J ++;

I ++;

}

 

Void main ()

{

Fun (); // call I ++ for the first time, But I is in the fun function. When the function ends, I will be released, but j is a static variable, it will be released only when the main ends. After j ++ is 2,

Fun (); // if you call it again, you will find that I is still 0 before ++, but j will be 2 after the last call,

}

(3) The static function has only one copy in the memory, and the normal function maintains one copy in each call.

3. c ++ static member variables and static member functions (this will tell you why static member variables are used and where to initialize them)

Http://blog.csdn.net/clc4210408/article/details/6775824#comments you can take a closer look at this article (there are errors in it, carefully look for a look !!)

Http://c.biancheng.net/cpp/biancheng/view/209.html

Everyone remembers that there is only one static object. No matter how many objects there are, there is only one static member variable and one function (every common member variable has one object and everyone does not affect each other ), as long as the value of the static member variable changes, it will be reflected in all objects,

Class Test

{

Public:

Int;

Static int B;

Test ();

Static int fun ();

};

Int Test: B = 1; // initialize the static member variable. The class size is not increased outside the class, and the static member variable cannot be added.

Int Test: fun ()

{

Returen B;

// Return a; this is incorrect, because static member functions can only access static member variables !!!

}

Test: Test ()

{

This-> a = 0;

}

Void main ()

{

Test t1, t2;

T1.a = 1; // at this time, you will find that the value of t2.a is still 0 and it will not change because t1.a is changed to 1.

T1. B = 2; // at this time, you will find that t2. B is also changed to 2, which is caused by static. The B pair of the object t1 and t2 is actually the same

}

Related Article

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.