Please enable JavaScript.
Coggle requires JavaScript to display documents.
Pointers, lifetime - Coggle Diagram
Pointers
smart pointers
deallocation of dynamic variable happens AUTOMAGICALLY after there are no more pointers to it
types
unique_ptr
there can be only one
owns the pointed data exclusive
move
transfers ownership
swap
reset
and
release
but prefer
move
:no_entry: can’t copy/assign
shared_ptr
multiple ok
reference counter
count how shared ptrs many point to object
pointed object lifetime ends when counter = 0
danger of circular pointers that are never deallocated after end of function
reset
shared_ptr<int> sp2 (sp1); //
copy constructor
shared_ptr<int> sp4 (move(sp3)); //
move constructor
copy and move assignment OK
weak_ptr
examines obj
doesn’t interfere with lifetime/ownership
compatible with shared pointers
assigning a shared pointer to a weak pointer ok
reset
and
expired
:no_entry: can’t move
:red_cross: no * or -> operators
dynamic variables
programmer allocates
no name
commmands
new
#
#
delete
forget = mem leak
normal variables
compiler allocates and deallocates
normal pointers
programmer allocates & dealloc
lifetime
dynamic
programmer does it
dynamic objects
accessed with pointers
static
compiler does it