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
0
votes
2 answers

How to access a range of bytes in a unique_ptr byte array?

Update Went for an alternate method of reading in See below Original post I'm still new to C++, I've been coding in Unreal for a bit, but I'm writing a File Parser and am doing it as a standalone solution. I want to read an unknown length of bytes…
r3dstar
  • 1
  • 1
0
votes
1 answer

How can I make a smart array pointer which stores the size?

I need a smart buffer that uses malloc and free such as for example: shared_ptr buffer((byte*) malloc(123), free) unique_ptr buffer((byte*) malloc(123), free); However I find it tedious and error-prone to…
0
votes
1 answer

Can I copy a `std::function` from a `std::unique_ptr` before moving it?

Consider the following hierarchy: class BaseICannotChange {}; class DerivedIControl: public BaseICannotChange { private: int _Value; public: DerivedIControl(int value): _Value{value} {} int getValue () const {return…
Antonio
  • 355
  • 1
  • 2
  • 13
0
votes
1 answer

How can I inherit all the unique_ptr constructors?

I'm trying to inherit unique_ptr just to add a constructor which allocates the array using malloc and sets a const size field. #include #include using std::function; using std::unique_ptr; using…
0
votes
1 answer

Passing the object in a unique pointer as a reference to the other objects' constructor

I am using a 3rd party library, which returns a newly created object as a raw pointer. I store it in a unique_ptr with a custom destructor to properly destroy the object by calling the library's custom destructor. However, this object will be used…
Thinium
  • 171
  • 1
  • 14
0
votes
0 answers

What should I call this unique-pointer-with-size structure?

I'm annoyed by how C++' standard library only offers some functionality via pointers rather than structures which keep both an address and a size. Specifically, suppose I want something like a unique_ptr, but which keeps the associated…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
1 answer

using pimpl pattern in templated class. How to allocate a unique_ptr

My colleague insists our project should be able to use a template like this: template class Foo { public: Foo(T t, B b); // some more functions private: struct pimpl; std::unique_ptr m_impl; }; I'm…
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
0
votes
0 answers

Memory leak error when trying to modify an unique_ptr inside a function

I am trying to build a little game "snake" playing by a neural network. I created 2 classes, one for the neural network and another one for the game. The game has an instance of the class neural network and I used an unique_ptr to avoid a basic…
PsyKozZ09
  • 21
  • 3
0
votes
1 answer

std::unique_ptr custom deleters seem to need a pointer argument -- is there an alternative that can take a non-pointer argument?

I was recently introduced to this mechanism of using a std::unique_ptr to implement a "generic" RAII mechanism: // main.cpp #include #include #include #define RAII_CAT(x) raii ## x #define RAII_CAT2(x)…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
0
votes
0 answers

accessing the nullptr data

I ran into a problem with an uninitialised unique ptr, here is the code: class Demo { public: Demo() {} void noSegFault() { std::cout << "noSegFault" << std::endl; } void segFault() { std::cout <<…
goodman
  • 424
  • 8
  • 24
0
votes
1 answer

unique_ptr versus heap allocating and delete

Say that I have a class A and a class B which further has a subclass B1. Now I want to assign a pointer of B1 to be a member of A. I could do that by simply heap allocating B1 and then using delete in the destructor of A as follows: class B { …
Chris Gnam
  • 311
  • 2
  • 7
0
votes
1 answer

Convert c++98 code to new c++17 code when using std::vector of pointers

i dont what to manage or less as i can manage raw pointers note in the example i have to delete the object before removeing it from the vector i want to avoid it it here and later what will be good case to convert this code to using unique_ptr or…
user63898
  • 29,839
  • 85
  • 272
  • 514
0
votes
0 answers

Why is this move assignement operator implicitly deleted for unique_ptr?

I have a type pod that I'd like to pass a handle around using a unique_ptr with custom deleter. Only it doesn't work because the move assignement operator is implicitly deleted. I don't understand exactly why? The deleter is a pod type which…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

"Nested" make_unique calls failing on std::uninitialized_copy()

New to C++ and mostly enjoying the learning curve, but struggling to solve this one. I'm needing to satisfy this requirement in two different related places std::unique_ptr [is typically used] as the element type in move-aware containers, such as…
markdwhite
  • 2,360
  • 19
  • 24
0
votes
0 answers

Can a unique_ptr be moved when referenced by a weak_ptr?

I have a bunch of unique_ptrs for database connections. They get checked out by different parts of the code to run database queries, then returned (std::move) back to the container for next use. I want to keep track of all instances of these…
Sean
  • 1
1 2 3
99
100