10. Chapter 10 pointer

1. Memory address each byte has an address variable is the first byte address. (Such as a type of int 4 bytes, it will be the first byte address in the entire address as a variable)

2. The array name is a constant pointer.

3. nullptr to indicate an invalid memory address.

4. pass a pointer to the function, and transfer the same effects can be cited. But easier to handle than the reference pointer, because it hides all the dereference and indirect reference mechanism. When handling strings c, the pointer is preferably used.

The pointers to const as parameter, both receiving the address constant argument, may also receive non-constant argument address. (Pointers to const can be interpreted as point constant data pointer, the data pointer is not changed, but the pointer itself may be changed while const pointer means is a pointer itself is a constant, after being initialized, the pointer value itself is not changed )

6. Dynamic memory allocation can only be achieved by a pointer, it may allow the program to create temporary variables necessary (done by the new operator, it is more creating a dynamic array).

7. Use new, will allocate memory from the heap, after use, you should use delete to free memory, otherwise there will be a memory leak. After calling delete on the pointer, the pointer should immediately assigned to nullptr, indicating that the pointer no longer points to a valid memory.

8. avoid dangling pointer method: (1) after the release of memory, the value of the pointer is immediately set to nullptr. (2) before attempting to access the value of the pointer, first determine whether the pointer is NULL. 

9. The function returns a pointer to dynamically allocated memory area pointer should not return a pointer to local variables.

10. It should be dynamically allocated call new class in its constructor and destructor call delete function.

11. The introduction of smart pointer to resolve dangling pointers, memory leaks, double delete other issues. Smart pointers have unique_ptr, shared_ptr, weak_ptr three types. The core idea is the question of ownership smart pointer dynamically allocated memory.

12. unique_ptr <int> int denotes a pointer pointing to the exclusive. Exclusive pointer objects that represent the pointer is one to one relationship, when it goes out of scope or to point to other objects, it will automatically release objects they manage. Using move () function, the transfer of ownership of an object from a pointer to another exclusive exclusive pointer. Because of this, when passing parameters, required move () function to transfer ownership (the argument by the value passed to the parameter, a pointer to cause two objects, this would violate the principle exclusive, and will not be passed by reference meeting)

13. in c ++ 14, may be used make_unique <T> () returns an object to the same token, can be used make_shared <T> ().

14. dynamically allocated shared pointer may be used to manage a plurality of smart pointers commonly owned objects. When using a shared pointer, two pointers should avoid the same set of circumstances managed object.

15. Use make_shared <T> is not only efficient, but also to avoid the occurrence of dual management.

 

Guess you like

Origin www.cnblogs.com/Hello-Nolan/p/12232756.html