Questions tagged [unique-ptr]

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer. unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object.

cppreference:

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.

std::unique_ptr was designed to replace the std::auto_ptr in C++03. It improves on the implementation of auto_ptr by implementing specific move semantics (it is not copyable) that were not available in the language of C++03.

std::unique_ptr, together with std::shared_ptr and (std::weak_ptr) form the core smart pointers used in C++ to implement RAII semantics, especially with respect to traditional memory management. With custom deleters, these smart pointers can also be used to manage other resources.

Resources:

2193 questions
1
vote
1 answer

C++11 - Possible dilemma with pimpl-idiom and unique_ptr?

As I was trying to take advantage of pimpl-idiom and smart pointers to implement my own wrapper around platform-specific GUI components, I encountered a problem I'm unable to solve. The problem is probably with pimpl-idiom and *unique_ptr*, since I…
Helixirr
  • 911
  • 9
  • 18
1
vote
2 answers

unique_ptr with deleter

I am trying to use std::unique_ptr with deleter. This is my code: template struct Deleter { void operator()(T* p) { delete[] p; } }; void Test() { vector> v; for(size_t i = 0; i < 5; ++i) …
Alex F
  • 42,307
  • 41
  • 144
  • 212
1
vote
4 answers

Getting out of a function, with reference, an object of a type with no default constructor

For example bool read(Input &input); Input input; //error bool success = read(input); will be an error because Input has no default constructor. Is there any trickery I can use to get the Input object out of the function in this case? I imagine…
user2015453
  • 4,844
  • 5
  • 25
  • 27
1
vote
1 answer

Swapping unique_ptr in a multidimensional c++ array?

I'm trying to refactoring the following code which relies on classic C-style arrays to make it more C++ like: fb::Block *blocks[Panel::X][Panel::Y]; void Panel::mechanics(int64_t tick) { for (int32_t y = 1; y < Panel::Y; y++) { for (int32_t x…
bquenin
  • 1,470
  • 2
  • 12
  • 12
1
vote
2 answers

error using static function deleter with std::unique_ptr

I have the following code snippet that fails to compile...seems like it should, but it evading me right now. Greatly appreciate any help/suggestions! #include #include struct MyClass { static void free_lock(std::atomic**…
outro56
  • 62
  • 7
1
vote
1 answer

Which would be better: A unique_ptr to a 2D array or a 2D array of unique_ptrs?

I'm currently in the middle of making an old project of mine memory-safe. In this project I have a 2D array populated with pointers to instances of my own class Block. declared like so: Block* gemGrid[xMax][yMax]; and populated later like…
Guy Joel McLean
  • 1,019
  • 4
  • 12
  • 34
1
vote
4 answers

unique_ptr, move constructor, and why always attempt to access private member

I am encountering this problem frequently and I believe a move constructor is in order but I think the copy constructor is the problem and hiding it does not seem to work. The code: template class LinkedList{ public: // …
user633658
  • 2,463
  • 2
  • 18
  • 16
1
vote
0 answers

Memory leak using unique_ptr

Hello I am having an issue where I seem to be leaking memory when I am handling my unique_ptr array variables. Now I have isolated where the issue is occurring, but I don't understand why the leak is occurring. I suspect I may be misusing the…
1
vote
1 answer

Broken std::unique_ptr in GCC 4.7.2

I'm attempting to use std::unique_ptr in order to store the integer handle to some opaque objects. For this purpose, I have defined a custom deleter type which does typedef int pointer in order to override the raw pointer type to int instead of…
pmjobin
  • 813
  • 1
  • 7
  • 15
1
vote
0 answers

Obtaining address of raw pointer contained in a unique pointer

I have a function, which takes a parameter which is a pointer to pointer. func(**pTmp); Actually this pTmp is a raw pointer and pTmp I get is a unique pointer, so I need to pass the address of the raw pointer contained in unique pointer. I do it…
yogesh singh
  • 101
  • 1
  • 9
1
vote
0 answers

How do I initialize a vector of unique_ptr objects?

Possible Duplicate: Can I list-initialize a vector of move-only type? I've tried this, but get complaints about calling ::std::unique_ptr's copy constructor. That doesn't seem right. :-( #include #include int main() { …
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
1
vote
1 answer

C++: Is it possible to have a template class which represents a conditional reference to a resource?

So sometimes I want an object to have a reference to a shared resource (let's say something of type A), or alternatively to have its own copy of an A. Furthermore the object may find itself inserted and manipulated inside of containers (vector,…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
1
vote
1 answer

managing raw memory with std::unique_ptr in C++

I want to have a little class which manages raw memory with this API template > class raw_memory { static_assert(std::is_same::value, "raw_memory:…
Walter
  • 44,150
  • 20
  • 113
  • 196
1
vote
1 answer

event_base* vs unique_ptr

The code I'm developing tries to avoid handling bare pointers, but I want to add some event based functinalities to it - I would use smart pointers here too, but as I understand it, this would mean that event_base will be deleted using delete…
Kim Strauss
  • 329
  • 3
  • 10
1
vote
3 answers

unique_ptr: How to safely share raw pointer

I am making an class which is managed by a unique_ptr, but for various reasons I need to give implementations access to a raw pointer to the object. However I want to ensure that users don't inadvertently delete the underlying object. I have come up…
Dan
  • 12,409
  • 3
  • 50
  • 87