Skip to content
AtomicReps

Navigation

14/30Computer 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

    After std::string t = std::move(s); where s held a 58-character string, what did s.size() return in the recorded run?

  • C++ Memory & Lifetime · 2 of 3

    RAII

    In C++, what does this program print for std::is_copy_constructible of a struct that owns a raw std::string* and deletes it in its destructor?

    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?

    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,316 C++ Memory & Lifetime questions.

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