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
1 answer

How can I use a lambda with captures as a deleter in std::unique_ptr?

I want to open a file and perform some operation on the file, the testOpen and testClose are wrappers around the API function test_open and test_close so that if there's an error in performing these operations I can throw a exception which would…
0
votes
0 answers

Klocwork raises warning on make_unique

I have the following structure, struct MyStruct{ bool myBool, int myInt, std::unique_ptr> myUniquePtr; } ; Let's say I construct the following object, MyStruct* myStruct = new MyStruct(); I then update the size of my…
floflo29
  • 2,261
  • 2
  • 22
  • 45
0
votes
1 answer

std::unique_ptr with custom deleter: regular vs array type

Say I have two functions: const char* get_string(int id); bool free_string(const char* str); I want to write a std::unique_ptr wrapper for them. From this answer I created the following: template struct deleter_from_fn { template…
Paul
  • 6,061
  • 6
  • 39
  • 70
0
votes
1 answer

Iterator design pattern using unique_ptr instead of raw ptr

I've got this fairly simple C++ code from refactoring.guru. It compiles and works as expected. However, it uses new and delete via the CreateIterator() member function, and I'd like to port it to use unique_ptr, but I'm not sure how to do that…
doneThat
  • 3
  • 2
0
votes
3 answers

Creating static unique_ptr with ternary operator

I have a Interface named IDBAbstractFactory and two derived class CPostgreSQLDBAbstractFactory and COracleDBAbstractFactory. I am trying to create a std::unique_ptr with below code which is not…
0
votes
1 answer

Will std::move() call the destructor of the owned object of a unique_ptr?

Consider the following code: #include #include #include struct Data { std::string name; Data(std::string aName): name(aName) { std::cout << name << " Constructor is called\n"; } ~Data() { …
Darshan Bhat
  • 245
  • 1
  • 11
0
votes
1 answer

Do I need `__stdcall` in `std::unique_ptr` deleter?

I want to manage a WinAPI function pair via std::unique_ptr: #include int* __stdcall construct(){ return new int{5}; } void __stdcall destruct(int* v){ delete v; } int main() { using Ptr = std::unique_ptr; Ptr…
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
0
votes
0 answers

Why does accessing the map in a part of code I'm not even actively using crash the program?

I've got this piece of code: class BuffComponent : public Component { public: using BuffMap = std::unordered_map>; BuffMap buffs; BuffComponent() {} BuffComponent(const…
aallfik11
  • 59
  • 5
0
votes
0 answers

How to use make unique to create a pointer to a base class instead of wrapping new in a unique pointer?

Say there are two classes, Base which contains certain parameters and D which contains more parameters. I need to make a variable with Base type then pass there type D which inherits from base. class Base { protected: Base(const String&…
jquigs62
  • 1
  • 1
0
votes
1 answer

Issues about using unique_ptr as type of vector (C++)

I‘m trying to understand a C++ code that uses vector which stores unique_ptr, where Base is a base class and has a derived class Derivate. When pushing unique_ptr into this vector, there is no errors and can correctly call the method…
0
votes
0 answers

When implementing Trie Data Structure using smart pointer is it necessary to use shared pointers?

Link to gfg: Trie Data Structure using smart pointer I came across this implementation of Trie Data Structure using shared pointers. But I don't understand the purpose of using shared pointers. Can we not simply use unique pointers here?
0
votes
0 answers

Is a node tree possible with a vector> collection?

Using unique_ptr is causing some issues within a node tree, producing a hard to find error in memory, Xutility, 2280 errors, citing a call to a deleted function (VS2022/C++20). The code is a typical node tree, where properties and nodes derive from…
S9DD
  • 3
  • 3
0
votes
2 answers

How to idiomatically store a unique_ptr or shared_ptr at runtime?

I have an instance of class Foo that will be passed a smart pointer to a dependency object. This may be a unique_ptr, if the caller wants to transfer ownership of the object to the Foo instance, or a shared_ptr if the caller wants to share the…
davidA
  • 12,528
  • 9
  • 64
  • 96
0
votes
1 answer

push objects in vector of class when class has a deque of unique_ptr

I am somewhat new to using unique_ptr and was using it in some of my code. While trying to use unique_ptr, I have the following code which doesn't seem to work, could anyone explain me why? #include #include #include class…
0
votes
1 answer

How to initialize a pointer or unique_ptr to a variant in C++

I am analyzing columns in one or more large data tables loaded from binary files. Every column can be one of several predefined types and is essentially a vector. I defined the column to be a variant of several vectors. In the toy example below, I…
Yassen
  • 1