C + + Interview Summary (1)

Source: Internet
Author: User

Recently looking for a new job, in the search for a job encountered a lot of interview questions, most of me embarrassed, again let me know that their lack of knowledge, the last job is to the status of fresh students, all at that time into the project team did not have a lot of requirements, into the project team after their own studious (I think), before there is no relevant Self-confident (in fact, are blind), in the last job encountered a few willing to take my old staff to help me very big (also indirectly caused oneself very arrogant), the previous work mainly use C + +, often use STL (feel oneself mastered very skilled, interviewer asked me map and list of what is the difference between and so on ...) A face is crazy!! Fortunately, I was lucky to find a job, the following are the interview questions I have summed up:

1. Byte alignment issues (almost every company's face), here are some of the concepts I've copied online and byte-aligned.

The first important concept is why byte-aligned? (first to understand this before I do not know, sometimes it is not know why this value)

(1) The root cause of byte alignment is the efficiency of the CPU access data, for 32-bit machines, 4-byte alignment can improve access efficiency, if the double type is 8 bytes beyond the 4-byte boundary storage, the CPU will read two times, the efficiency will be lower, in the VC default is 4-byte alignment, GNU gcc is also the default 4-byte alignment.

What are the criteria for understanding byte alignment before you solve these problems (you can do it by yourself. Byte alignment)

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

(2) The offset of each member of the struct relative to the first address of the struct is an integer multiple of the member size (if not, the compiler fills the bytes among the members to align).

(3) The total size of the struct is an integer multiple of the size of the structure's widest base type member. (if not, the compiler populates the bytes after the last member).

struct Test

{

char T1;

Double T2; The offset must be its own integer multiple, and sizeof (double) is 8, so according to the (2) guideline, the 7 bytes are to be populated between T1 and T2

The offset of int t3;//t3 is 16 bytes, sizeof (int) is 4,16 is its multiple, the total number of bytes is 1+7+8+4=20, according to (3) The guideline must be a multiple of 8, all T3 need to fill 4 bytes after

};

sizeof (Test) = 24;

What if it's a union? Take a look at the following example

typedef Union

{

Char A;

int b[5];

Double C;

};

We know that the size of the union is the size of its widest base type member, which is obviously the width of the array is 20 bytes, then the size of this Union is 20? Obviously not, because the size of the union, in addition to the widest base type, should also be a multiple of the size of all base type members, all of which need to be populated with 4 bytes, so the size of this union is 24.

A Union can also be a member of a struct, and many of these questions can be found on the web.

http://bbs.csdn.net/topics/360183704 This blog you can also look at your interview will certainly be helpful.


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

(1) They differ in scope, the scope of the static global variable is limited to the file that declares it, the non-static modified global variables are also available through extern in other files, they are stored in a static way,

(2) The static local variable is initialized only once, the next time the value used depends on the last value, the static local variable is destroyed at the end of the program, and the local variable is freed after its scope ends.

void Fun ()

{

int i = 0;

static int j = 1;

j + +;

i++;

}

void Main ()

{

Fun ();//First Call i++ but the scope of I is in the fun function when this function ends I will be released, but J is a static variable, only used when main ends will be released, J + + is 2,

Fun ();//Call again you will find I before + + or 0, but J will be the last call after the value of 2,

}

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

3.c++ static member variables and static member functions (which will let you talk about why static, where to initialize)

Http://blog.csdn.net/clc4210408/article/details/6775824#comments we can take a closer look at this article (there are mistakes, look carefully!!) )

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

Everyone remembers that there is only one copy of static, no matter how many objects, static member variables and functions only one copy (normal member variables each object will have one copy and everyone does not affect), as long as the static member variable value changes will be reflected in all objects,

Class Test

{

Public

int A;

static int b;

Test ();

static int fun ();

};

int test::b=1;//Initializes a static member variable that does not increase the size of the class outside the class, and cannot be added static

int Test::fun ()

{

Returen b;

return A; This is wrong, 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 point you will find that the value of t2.a or 0 will not be changed because t1.a becomes 1.

t1.b = 2;//At this point you will find t2.b how also become 2, this is static caused, the object t1,t2 to the B is actually the same

}

C + + Interview Summary (1)

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.