C ++ interview preparation

Cited differences and connections with the pointer
  1. A pointer is an entity, it is assigned memory, and can allow multi-level pointer
  2. Reference must be initialized when created and immutable (can only be initiated once it is not const), but no need to initialize the pointer to create the best initialization to prevent NULL
  3. The two different increment (+) result, a reference value is incremented, and the address pointer is incremented;
  4. Sizeof different results, obtained sizeof reference is the size of the variable pointed (object), sizeof pointer and a pointer size obtained is itself
  5. Reference access is direct access to the original object, the pointer is accessed indirectly
  6. Passed as a parameter to a function of different parameters, reference than the participants pass a pointer parameter passing more secure, see next ask
  • contact
  1. Internal references implemented using pointers
  2. Reference is subject to the restrictions of the pointer
Why pass by reference safer than pass a pointer?
  1. Reference must be initialized at the same time created to ensure that the object reference is valid, so NULL reference does not exist; and when the pointer does not have to define initialization, so it may be a NULL pointer, can be reassigned at any place behind the definition
  2. Once a reference is initialized to point to an object, it can not be changed to another object reference; and the pointer to point to another object may be changed at any time
  3. Quote creation and destruction will not copy constructor calls the class
    because there is no null references, and references Once initialized to point to an object, it can not be changed references to another object pointer so than safety.
    Because const pointer remains a null pointer, and it is possible to produce field guide, so it is still unsafe. The solution is smart pointers.
Introduce smart pointer

Smart pointers, the basic package type pointer to class object pointer (this class is definitely a template to suit different types of basic needs), and write delete statement deletes memory pointer in the destructor.
The object is to create smart pointer on one kind of stack, will be called when the function exits its destructor, the destructor which is often a bunch of counting conditions such judgment, if it reaches a certain condition, put the pointer to the real space to release.
Use an ordinary pointer, likely to cause heap memory leaks (forgetting to release), the second release, wild pointers, memory leaks and other problems occur such as abnormal procedures, the use of smart pointer to better management of heap memory.

Commonly used smart pointers

Smart pointers in C ++ after version 11 offers, contained in the header file In, shared_ptr, unique_ptr, weak_ptr.
In C ++ 98, there is std::auto_ptr , but it has a lot of problems. It does not support copying (copy constructor) and assignment (operator =), but when copying or assignment will not prompt an error. It may cause the program to crash.

auto_ptr<string> p1(new string ("auto") ; //#1
auto_ptr<string> p2;                                   //#2
p2 = p1;                                                      //#3

After the statement # 3, p2 took over ownership of the string object, p1 of ownership will be deprived prevent destructor p1 and p2 trying to delete the same - objects;
if then access the content p1 points will cause the program to crash, because p1 no longer point to valid data. std::auto_ptrIs replaced unique_ptr.

  1. unique_ptr
    a time, only one unique_ptr pointing to a given object.
    Does not support copying and assignment, but better than auto_ptr, direct assignment will compile error. Really want the assignment, then, you need to use std::move.
  2. shared_ptr
    based on reference counting smart pointer, providing a shared ownership smart pointers, releasing the shared object when the last shared_ptr pointing to a shared object is destroyed.
  3. weak_ptr
    mentally able pointer to the object, it does not control the lifetime of the object pointed to by the smart pointer to a shared_ptr smart pointer by the management. A weak_ptr bound to a shared_ptr object shared_ptr does not change the reference count. Once the last of the object pointed shared_ptr is destroyed, pointed object will be released, even though at this time there is weak_ptr point to the object, the object pointed to still be released.

Guess you like

Origin www.cnblogs.com/lvjincheng/p/11317105.html