Skip to content
AtomicReps

Navigation

14/29Computer Science

C++ Memory & Lifetime.

Ownership in C++: RAII, the smart pointers, copies against moves, and the lifetime rules.

  • C++ Memory & Lifetime · 1 of 3

    Move Semantics

    A moved-from standard string is measured. What does it report?

  • C++ Memory & Lifetime · 2 of 3

    RAII

    What does this program print for the class holding a raw owning pointer?

    typescript
    #include <iostream>#include <string>#include <type_traits>struct Five {  std::string* p;  Five() : p(new std::string("x")) {}  ~Five() { delete p; }};int main() {  std::cout << std::boolalpha << std::is_copy_constructible<Five>::value;}
  • C++ Memory & Lifetime · 3 of 3

    shared_ptr & weak_ptr

    What does this program print for the holder of itself stored in a member?

    typescript
    #include <iostream>#include <memory>struct S : std::enable_shared_from_this<S> {  std::shared_ptr<S> self;  void keep() { self = shared_from_this(); }  ~S() { std::cout << "s"; }};int main() {  {    auto s = std::make_shared<S>();    s->keep();  }  std::cout << "|after";}

Three of the 1,320 C++ Memory & Lifetime questions.

Keep going with C++ Memory & Lifetime, free
Next topic · 15/29C++ Programming.