Interview written summary (i) C + + Foundation

Source: Internet
Author: User

C++

1. Smart pointer memory management garbage collection Pointer problem resource management (memory is resource )

Mechanisms that can be counted by reference ... Implement memory reclamation and do not let memory leak.

Issues related to memory leaks:

    1. When an object is created (new) and the object is used, a memory leak occurs without a delete.
    2. Or there are a lot of pointers pointing to the same piece of memory, and when one is finished with the delete, the rest of the pointers are nowhere to be pointed.
    3. A pointer leak caused by a circular reference

Many people forget that smart pointers are useful at this point, helping programmers to automatically release them in destructors. Two classes:

First question idea: The pointer to the smartpointer is the pointer to the object

    • Smartpointer class constructor destructor: There is a constructor of its own that will pass the object pointer over to itself, destroy time delete oneself is OK.
    • There is a private member: The pointer of object ptr.
    • The completion of the construction is equivalent to the PTR of object is the P of object.
    • So releasing your PTR is equivalent to destroying the P of object;

Class Object {
Public
int A, B;
};
int main () {
while (true) {
Smartpointer P (new Object ());
Process (P);
}
}
void Process (Object &p) {
Do something;
}
Class smartpointer{
Public
Smartpointer (Object *p) {
Ptr=p;
}
~smartpointer () {
Delete ptr;
}
Private:object *ptr;
};

Second question:

reference count class ~~~~~~~~~~~!!!!!!!!!! Records how many pointers are currently pointing to this memory.

STL is not based on reference counting: Outo_ptr at the same time a pointer can only have one object owned by one ... Disadvantage: Cannot assign a value operation, cannot pass the parameter ~,

Count-based: shared_ptr!!!!!!!!! of the Boost library

May be in:

Construct, copy, assign = 

Problems with pointer pointing and pointer count occurring

Defines a pointer to counter Smartpointepro

Defines a pointer to memory (object) counter

A friend class can access members of the Counter class

Class Object {Public:int A, b;};        int main () {while (true) {Smartpointer P (new Object ());    Process (P); }}void Process (Object &p) {//do something;} Class conter_smartpointer{
Public:conter_smartpointer (Object *p) {//Common constructs generate new memory csp=new Counter (p); } conter_smartpointer (Conter_smartpointe &p) {//points to the same memory, duplicates CSP=P.CSP; csp->cnt++; } conter_smartpointer& operate= (Conter_smartpointer &p) {//Assignment//right++ left--csp->cnt--; to the right Cover off, pointing to the right side of the memory of the original reference count minus one p.csp->cnt++; if (csp->cnt = = 0) {Delete cSp; } CSP=P.CSP; } ~conter_smartpointer () {csp->cnt--; if (csp->cnt = = 0) {Delete cSp; }} Private:counter *csp;//points to the same Counter};class Counter () {
Friend class Conter_smartpointer;//conter_smartpointer can access members of the counter class (including private members)
Pravite:int Cnt;object *ptr; Counter () {p=null; cnt=0;} Counter (Object *p) {ptr = P; Cnt=1;}

  

Question three:

The father pointed to the son, the son pointed to the father, no one first released. Ring

Solution: Boost::weak_ptr

Assignment passing: Copying an incoming argument ~ processing in a function does not affect the original object

Reference: Opposite

Resource Management (memory is a resource ):

Use the packaging of resources in C + + to prevent memory leaks

2. Single-case mode

Design mode:

A class can only have one object ~

Applied to: The output of the system log, an interface with only one mouse mouse class.

1. A class can only one object ~

2. Release of Memory

3. Thread safety, multiple classes produce an object

What's the problem:

Class Singleton () {private:     Singleton () {    }
   Static singleton* instance;public: static singleton* getinstance () { if (instance = = NULL) { instance = new Singleton ();}}}

Want to produce a class can only be passed getinstance ()

Make the constructor private so that no other class can access the constructor

Problems with function passing and assignment: problems that may result in multiple objects

No destruction: Because there is no object: Static defines a class that belongs to the entire class and does not have a destructor. Static is not allowed to be modified once it is constructed. In the static storage area.

For the problem of freeing memory, new is generated in the heap and must be released manually

Staic in a static store so that the operating system automatically frees memory

Method one: How to ensure thread-safe && memory without manual release

static Singleton instance; Use instance directly when calling again: guaranteed to produce only one object

class Singleton () {private:     Singleton () {    }    &p) {    }     Singleton & operate = (const Singleton &p) {    }   public:     static singleton* getinstance () {                 static  Singleton instance;         return &instance;    }    }

Method Two: Ensure thread safety

Double check plus lock

Postscript:

Tomorrow is 11 after the first business day, in my impression HR said will give me notice ... Ah ~ At that time also did not listen carefully, after all, is the first interview, directly 3 side I was also very surprised ~ also a bit of a Meng, because I do not even know what to interview .... Hey, hope to have a good result.

For the protection of the study for a year, then heard the policy when the silly dropped, did not cover the research. Did not cry, the next day I immediately adjust the mood to decide I want to find a job, not postgraduate!!! Although a question has not been brushed. Heart still panic, just in leetcode school code. When I learned half of it, I suddenly found that many companies were gone.

11 Holidays, I also enrich themselves, below to make a summary of it. First summarize the basic C + +, after all, the second interview is in C + + on the basis of hanging ... I am sorry for the one side of the seniors to send my doll .... Hey...

It's so cold and cold!!!

Interview written summary (i) C + + Foundation

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.