- Default vs Value iniatilization:
Default initialization doesn't overwrite remaining stack bits.
Value initalization initializes the array with a clean stack and overwrites all entries with 0.
# default initialization int *foo = new int[length]; foo[0] # != 0
# value initialization int *foo = new int[length]; foo[0] # == 0
- Use C++11 smart pointers instead of
new
anddelete
.#include <memory> { // Use make_shared when possible. foo ref count is 1. std::shared_ptr<Foo> foo = std::make_shared<Foo>(); { std::unique_ptr<Bar> bar = std::make_unique<Bar>(); // Initialize with copy constructor. Increments ref count of foo to 2. std::shared_ptr<Foo> foo2(foo); } // foo is deallocated here. // bar has ref count 1 and points to an existing Foo instance. } // bar is deallocated here.
Data Structures | Algorithms | Concepts |
---|---|---|
Breadth-First Search | Bit Manipulation | |
Depth-Frist Search | Memory (Stack vs. Heap) | |
Stacks & Queues | Binary Search | Recursion |
Heaps | Merge Sort | Dynamic Programming |
Vector/ArrayLists | Quick Sort | Big O Time & Space |