Dynamic Memory and Smart Pointers
shared_ptr:
init: make_shared< Type >(args); shared_ptr< Type >(q); p=q;
function :
p.unique() if count return 1 truw,else false.
p.use_count() return the number of object sharing with p.
make_shared. allocates and initializes an object in dynamic memory and returns a shared_ptr that points to that object.shared_ptr<string> p4 = make_shared<string>(10, '9');
new returns a pointer to the object it allocates:int *pi = new int;
By default, dynamically allocated objects are default initialized, which means that objects of built-in or compound type have undefined value
Like any other const, a dynamically allocated const object must be initialized.const string *pcs = new const string;
Functions that return pointers (rather than smart pointers) to dynamic memory put a
burden on their callers—the caller must remember to delete the memory
Dynamic memory managed through built-in pointers (rather than smart pointers) exists until it is explicitly freed.
three common problems with using new and delete
- Forgetting to delete memory. Neglecting to delete dynamic memory is known as a “memory leak,”
- Using an object after it has been deleted.
- Deleting the same memory twice.
dangling pointer:When we delete a pointer, that pointer becomes invalid.but the pointer continues to hold the address of the (freed) dynamic memory.
we cannot implicitly convert a built-in pointer to a smart pointer; we must use
the direct form of initialization to initialize a smart pointer
use reset to assign a new pointer to a shared_ptr.
In particular, classes that are designed to be used by both C and C++ generally require the user to specifically free any resources that are used.
unique_ptr:”owns” the object which it points.
so it doesn`t support ordinary copy and assignment.
but can transfer ownership fron one (nonconst)unique_ptr to another by calling release or set.// transfers ownership from p3 to p2
p2.reset(p3.release());
// reset deletes the memory to which p2 had pointed
only one unique_ptr at a time can point to a given object.
unique_ptr destroyed so the object it point to.
u.release() :return the pointer u had held and makes u null;
u.reset() :deletes the object to which i pointes;
u.reset(q) :if the built-in pointer q is supplied ,makes u pointe to that object.
u=nullptr; deletes the object to which u points,makes u null;
unique_ptr : bind to a pointer
http://stackoverflow.com/questions/6876751/differences-between-unique-ptr-and-shared-ptr
weak_ptr
not control the lifetime of the object it point to.
bind a weak_ptr to a shared_ptr not change the reference count of that shared_ptr.
weak:Once the last shared_ptr pointing to the object goes away, the object itself will be deleted. That object will be deleted even if there are weak_ptrs pointing to it.
The lock function checks whether the object to which the weak_ptr points still exists. If so, lock returns a shared_ptr to the shared object.
Dynamic Arrays
Classes that allocate dynamic arrays must define their own versions of these operations to manage the associated memory when objects are copied, assigned, and destroyed.int *pia = new int[get_size()];
// pia points to the first of these ints
The size inside the brackets must have integral type but need not be a constant.
Allocating an Array Yields a Pointer to the Element Type
shared_ptrs provide no direct support for managing a dynamic array. If we want to use a shared_ptr to manage a dynamic array, we must provide our own deleter:shared_ptr<int> sp(new int[10], [](int *p) { delete[] p; });
sp.reset(); // uses the lambda we supplied that uses delete[] to free the array
classes that do not have default constructors cannot be dynamically allocated as an array.
allocator: provides type-aware allocation of raw, unconstructed, memory.
We must construct objects in order to use memory returned by
allocate. Using unconstructed memory in other ways is undefined.