Embercode
✕
Pointers and Memory AddressesQuick quiz

Ownership and leaks

7 questions. Covers Leaks and detection.

Questions 1 to 5 of 7

  1. 1. Two shared_ptr objects point at each other: x->other = y; and y->other = x;. When the block that holds x and y ends, what happens?
  2. 2. In a tree where each parent owns its children, which link should be a weak_ptr?
  3. 3. What is the bug in this code? ``cpp std::shared_ptr<int> owner(new int(7)); std::weak_ptr<int> watcher = owner; int* raw = watcher.lock().get(); owner.reset(); std::cout << *raw << std::endl; ``
  4. 4. Which block leaks in this code, and why? ``cpp int* p = new int(5); p = new int(6); delete p; ``
  5. 5. True or false: a clean LeakSanitizer report, with no leaks found, means the program uses memory well.

5 question(s) left on this page.