diff --git a/README.md b/README.md index 5c640db..5e4d94a 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,42 @@ N::T t; // Use name T in namespace N using namespace N; // Make T visible without N:: ``` +## `memory` (dynamic memory management) + +```cpp +#include // Include memory (std namespace) +shared_ptr x; // Empty shared_ptr to a integer on heap. Uses reference counting for cleaning up objects. +x = make_shared(12); // Allocate value 12 on heap +shared_ptr y = x; // Copy shared_ptr, implicit changes reference count to 2. +cout << *y; // Deference y to print '12' +if (y.get() == x.get()) { // Raw pointers (here x == y) + cout << "Same"; +} +y.reset(); // Eliminate one owner of object +if (y.get() != x.get()) { + cout << "Different"; +} +if (y == nullptr) { // Can compare against nullptr (here returns true) + cout << "Empty"; +} +y = make_shared(15); // Assign new value +cout << *y; // Deference x to print '15' +cout << *x; // Deference x to print '12' +weak_ptr w; // Create empty weak pointer +w = y; // w has weak reference to y. +if (shared_ptr s = w.lock()) { // Has to be copied into a shared_ptr before usage + cout << *s; +} +unique_ptr z; // Create empty unique pointers +unique_ptr q; +z = make_unique(16); // Allocate int (16) on heap. Only one reference allowed. +q = move(z); // Move reference from z to q. +if (z == nullptr){ + cout << "Z null"; +} +cout << *q; +``` + ## `math.h`, `cmath` (floating point math) ```cpp