4

Does it ever make sense to create a std::unique_ptr using new? In the snippet below as I suspected the SimpleClass object being managed by the std::unique_ptr won't be destroyed unless I delete the std::unique_ptr myself. I couldn't think of a situation where this is useful so I was wondering if there are situations where this is actually used.

  std::unique_ptr<vector_test::SimpleClass>* ptr_to_unique_ptr = new std::unique_ptr<vector_test::SimpleClass>();
  ptr_to_unique_ptr->reset(new vector_test::SimpleClass(555));
  delete ptr_to_unique_ptr;
Mike Sweeney
  • 1,896
  • 2
  • 18
  • 20
  • I cannot think of a good use case for a raw pointer to a unique pointer to an object. If I were to find such a use case, I think I would try to use a unique pointer to the unique pointer to the object. – Eljay Feb 16 '19 at 17:58
  • 1
    What about using `std:unique_ptr>`? – πάντα ῥεῖ Feb 16 '19 at 17:59
  • 2
    I can only think if you need shared ownership of the unique pointer... https://stackoverflow.com/questions/50831775/making-shared-ptr-lose-ownership-of-memory – Galik Feb 16 '19 at 18:10
  • @Galik: And https://stackoverflow.com/a/28955443/103167 and https://stackoverflow.com/a/45510414/103167 – Ben Voigt Feb 16 '19 at 18:22
  • I think this could just be named misuse. Every pros given by RAII would be neutralized. – fiorentinoing Feb 16 '19 at 20:22

3 Answers3

2

Does it ever make sense to create a std::unique_ptr using new?

Most probably not.

There's no concise reason why you would/should fall back to manual memory management while already using the standard dynamic memory allocation techniques.

I couldn't think of a situation where this is useful so I was wondering if there are situations where this is actually used.

I can't timagine either about such situation.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

There is rarely a use for dynamically allocating a singular pointer. Closest real word use case is a singly linked list, where we dynamically allocate a class instance that contains a pointer, and some data associated with the node. Although, need to implement a linked list is rare because it is rarely the optimal choice of data structure, and because the standard library already provides decent designs of linked list.

Note that if we were to dynamically allocate a (smart) pointer, there is no good reason to not use a smart pointer to manage that allocation.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Does it ever make sense to create a std::unique_ptr using new?

I can't think of. There are situations where you want to prevent an object managed through a unique_ptr from deletion, when the scope that contains the unique_ptr ends. There are several ways to do this, without having to new the unique_ptr itself.

Some examples:

  1. Move the object to another unique_ptr in a different scope.

    std::unique_ptr<int> p1;
    {
        std::unique_ptr<int> p2 = std::make_unique<int>( 42 );
        p1 = std::move( p2 );
        // Destructor of p2 won't delete object, because ownership was transferred.
    }
    // Scope of p2 has ended, but object is still managed by p1.
    std::cout << *p1;
    
  2. Return an object managed through a unique_ptr from a function.

    std::unique_ptr<int> MyFun() {
        std::unique_ptr<int> p = std::make_unique<int>( 42 );
        return p;  // No explicit move required, because of RVO
    }
    
  3. Release ownership and get the raw pointer back.

    std::unique_ptr<int> p = std::make_unique<int>( 42 );
    some_C_API_that_takes_ownership( p.release() );
    // Destructor of p won't delete object, because ownership was given up.
    
zett42
  • 25,437
  • 3
  • 35
  • 72