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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
79 views

emplace unique_ptr in list

I am trying to emplace an std::unique_ptr into a std::list, but it's failing with the following error: <source>:29:23: error: no matching function for call to 'std::__cxx11::list<std::...
51k's user avatar
  • 1,393
1 vote
0 answers
40 views

How to deal with libraries requiring unique_ptr as inputs in pybind11?

C++ libraries that involve dynamic runtime polymorphism may choose to use std::unique_ptr to pass instances around. Given a basic type hierarchy like: struct Animal { virtual ~Animal() = default; ...
bluenote10's user avatar
  • 24.2k
0 votes
1 answer
130 views

Destructor called unexpectedly

Why is the destructor ~TestClass() in this simple code called twice? #include <memory> #include <iostream> #include <vector> class TestClass { int m_val; public: TestClass(int ...
Jabberwocky's user avatar
  • 48.8k
1 vote
1 answer
42 views

Calling ::GetModuleHandleEx with unique_ptr having custom deleter to handle freeing resources automatically

Do I need to free the HMODULE in the below code? DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS; HMODULE hm = 0; ::GetModuleHandleExW(flags, L"Test.dll", &hm); HRSRC hResource =...
Vk0726's user avatar
  • 13
-1 votes
0 answers
50 views

Why can not I initialize a polymorphic vector using unique pointers to child classes [duplicate]

#include <iostream> #include <memory> #include <vector> class Base {}; class Child1 : public Base {}; class Child2 : public Base {}; int main() { std::vector<std::...
virus00x's user avatar
  • 741
0 votes
0 answers
66 views

C++ template function, inheritance, shared_ptr

I guess this is a very basic question but I'm still asking because I suspect the compiler acts as expected and the error rather comes from me! I'd like to implement an abstract class with a template ...
duncan's user avatar
  • 27
0 votes
1 answer
99 views

Casting std::unique_ptr<uint8_t[]> to struct unique_ptr without losing uint8_t[] buffer

Is there a way to cast a std::unique_ptr<uint8_t[]>(n) to std::unique_ptr</*some_struct*/> so that struct points to the same address as uint8_t ptr was as well as saving that buffer? auto ...
Maga's user avatar
  • 3
1 vote
2 answers
154 views

Unwanted copy constructor call when creating a shared_ptr

While doing some coding practice, I encountered the following error when attempting to pass ownership of an optional std::unique_ptr to a std::shared_ptr: /usr/include/c++/11/ext/new_allocator.h:162:...
Damian's user avatar
  • 198
0 votes
0 answers
32 views

boost serialization on objects containing other objects ponted to by unique_ptr [duplicate]

I have an object of the class A which is a composition of several other objects from different classes X, Y, Z, ... Those classes are pointed to through unique_ptr. Lets assume that the sub classes X, ...
JamalNewtron's user avatar
0 votes
0 answers
153 views

c++ std::unique_ptr in a loop - memory leaks

Is unique_ptr used properly in this loop? Each time this while loop ends, the overall heap size increases slightly. As I investigate the code with debug tool of Visual studio, it seems that the ...
Hikaru's user avatar
  • 1
0 votes
2 answers
48 views

Question about unique pointer of classes (initialisation of variables in .h VS in constructor)

i'm a begginer starting with unique pointer unique_ptr and I have a question about them : If I've got a class A and inside it's .h I have an other class B like this : class A{ B b; public: A()...
Baptiste Girard's user avatar
2 votes
1 answer
64 views

Cast vector data of unique_ptr to pointer to const pointer

I have a std::vector of std::unique_ptr and I want to call a function from an API that takes a pointer to const pointer. Would it be correct to cast its data() to that type? This example program seems ...
jdehesa's user avatar
  • 58.7k
0 votes
0 answers
59 views

Boost Bimap with unique_ptr

I am trying to insert unique_ptr into the boost::bimap, but i am getting the error "call to implicitly deleted copy constructor". I am moving the unique_ptr through std::move and i have move ...
Tharani B's user avatar
1 vote
1 answer
89 views

Vector of polymorphic objects and unique_ptr

I am trying to do an assignment and decided to properly manage memory this time but ended up with more questions than answers. I want to have a vector of children of a parent class for example vector&...
Different's user avatar
0 votes
1 answer
102 views

How I can fill a unique_ptr that is pointing to a char array? [closed]

It's possible to put something in to an unique_ptr that is pointing to a char array, something like this: #include <iostream> #include <memory> #define LEN 100 using std::cout, std::cin; ...
Giovanni Scordilli's user avatar
1 vote
1 answer
86 views

Passing unique_ptr with derived class causes SEGFAULT

could someone explain to me why executing function foo() here causes SEGFAULT? When I change the unique_ptr object in D class to contain B class object instead of A, everything works ok. class A { ...
szj's user avatar
  • 13
2 votes
4 answers
138 views

Is there a way to share part of a shared_ptr in different ranges?

So, if you wanted to share everything in a shared_ptr past element x you could write something like the following: int main(){ std::shared_ptr<float[]> _vals = std::make_unique<float[]>...
Sam Moldenha's user avatar
2 votes
2 answers
75 views

Different scenerios of custom deleter for unique_ptr

was reading Nicholai Jossutis, STL book. There is a part like D d; // instance of the deleter type unique_ptr<int, const D&> p4(new int, D()); // ...
UPinar's user avatar
  • 1,098
3 votes
1 answer
92 views

How does the std::unique_ptr&& function argument work? [duplicate]

I'm writing a simple game engine in C++. I'm very late to C++, because I've spent most of my free time with C, so the unique_ptr, shared_ptr and all of this ownership magic is hardly understandable ...
y3v4d's user avatar
  • 195
1 vote
1 answer
152 views

Unexpected compile error when dealing with vector of unique_ptr<pure_virtual_class> inside std::map

#include <iostream> #include <algorithm> #include <vector> #include <map> class Base { public: virtual ~Base() = default; virtual void doIt() = 0; public: int base = 1; ...
undersilence's user avatar
0 votes
0 answers
57 views

Using unique_ptr in a factory method [duplicate]

I'm developing a message system that has to be able to handle different types of messages with different fields and values depending on an ID unique to each type of message. To do so I've created a ...
Luis Fernández García's user avatar
2 votes
2 answers
97 views

How can I concatenate several vectors of unique pointers

The original use-case The usecase is that I have a function to retrieve some factories derived from FooFactory, std::vector<std::unique_ptr<FooFactory>> facts = getFactories(); and each ...
Enlico's user avatar
  • 23.9k
0 votes
1 answer
101 views

How to use smart pointer to manage existing object

Suppose I have a vector of data generated from some API. It's for plotting only. vector<double> LineData = generateData(); I also have a class that do the plotting and manage the data class ...
qiu's user avatar
  • 31
1 vote
2 answers
120 views

C2661 when using std::make_unique

I have an error when I am trying to use std::make_unique. I probably have made a very simple mistake and I would be thankful if someone pointed it out. I am using GLAD and GLFW as 3rd party libraries. ...
Cake's user avatar
  • 43
2 votes
2 answers
83 views

Is there any idiom way to cast between `unique_ptr<T>*` and `unique_ptr<const T>*`?

I have an array of type shared_ptr<T> (or unique_ptr<T>). I want to use this array as a parameter for a function which will not modify its contents, so I want the input parameter type to ...
Zz Tux's user avatar
  • 610
2 votes
1 answer
77 views

Convert std::unique_ptr of derived to base and pass ownership as argument

I have this code: #include <iostream> #include <memory> class Base { public: virtual void doSmth() = 0; }; class Der1 : Base { public: void doSmth() final { std::cout ...
SavedowW's user avatar
-2 votes
1 answer
129 views

How does std::unique_ptr set the other pointer to null when moving?

When a move happens, usually a class sets the other's pointer to null, and this is the case with std::unique_ptr. The assignment operator is templated so that you can move unique_ptrs to different ...
Zebrafish's user avatar
  • 12.1k
6 votes
1 answer
104 views

Memory leak using unique pointer and polymorphism

I know that std::unique_ptr doesn't guarantee memory safety and especially it is true in the case of circular dependency. However, in my case, I can't see it. Child contain Parent(or SecondChild in ...
MegaRaman's user avatar
  • 100
1 vote
1 answer
98 views

Using std::unique_ptr<std::barrier<void()>> in a class - Error occured when calling arrive_and_wait()

Because I can't use directly std::barrier in a class, I decided to use std::unique_ptr in order to dynamically allocate it. That will also allow me to dynamically change the expected number of thread. ...
Ibrahima Keita's user avatar
2 votes
1 answer
94 views

Why the ownership of unique_prt is not transfered when using rvalue reference?

If I run the code class Myc { private: int m{0}; public: Myc(int ii) : m{ii} {}; ~Myc() { std::cout << "Myc Destructed\n"; } int getM() const { return m; } }; ...
Roberto Dias Algarte's user avatar
0 votes
1 answer
125 views

How to dereference std::unique_ptr<int[]>?

The operator* works for std::unique_ptr<std::array<int, 5>> but not for std::unique_ptr<int[]>. But why? Coming from cppreference: These member functions are only provided for ...
digito_evo's user avatar
  • 3,276
2 votes
2 answers
110 views

Understanding std::move and Ownership Transfer with std::unique_ptr in C++

I have written two sets of functions, F1/test1 and F2/test2 and I'm trying to understand the differences in the behavior of std::move and the transfer of ownership with std::unique_ptr in these ...
Sami's user avatar
  • 596
0 votes
0 answers
66 views

Cannot instantiate the abstract class

Here are my projects files compiled with VS: Init.h #pragma once #include <iostream> #include <memory> #include "Ishare.h" using namespace std; class Init { private: std::...
chandu's user avatar
  • 75
1 vote
1 answer
120 views

Convert a raw array pointer to a unique_ptr

I have trouble working with an unique_ptr. I am trying to work with the winapi and I succeeded to get the pixels from a bitmap with CreateDIBSection(). To have the pixels we have to create a BYTE* ...
PsyKozZ09's user avatar
1 vote
1 answer
68 views

How to prevent unique pointers from overlapping

I'm trying to build a timer in C++. In my Timer class there are two Date objects that hold a std::unique_ptr<struct tm> pointer. After I std::move the second unique_ptr in the second Date object ...
loremol's user avatar
  • 25
2 votes
1 answer
96 views

pimpl, std::unique_ptr and constexpr constructor

I'm reviewing a non-compiling code where I find a design similar to this: B.h #include <memory> class A; class B { private: int val; // pImpl idiom std::unique_ptr<A> ...
Oersted's user avatar
  • 1,058
4 votes
1 answer
103 views

Passing a static operator() as deleter type

Is the following code snippet legal in C++23? #include <memory> #include <cstdio> int main() { struct custom_deleter { static void operator()(int* const ptr) { ...
digito_evo's user avatar
  • 3,276
2 votes
1 answer
109 views

node-handle vs std::unique_ptr

Reading the documentation of the node-handle, I noticed that many features of the node-handle type can simply be emulated by a specialization of the std::unique_ptr. Indeed, the functionalities of the ...
LoS's user avatar
  • 570
0 votes
1 answer
59 views

Insert the whole vector<unique_ptr> as element in other vector

I tried to define move constructor Person(Person&&) and delete default one Person() = delete, but I still get std::unique_ptr<Person,std::default_delete<Person>>::unique_ptr(const ...
theateist's user avatar
  • 14k
0 votes
0 answers
39 views

Bison C++ invalid use of unique_ptr

I am writing a bison file for parsing. In a declared grammar rule, I'm trying to create a unique_ptr pointing to a syntax tree node (DeclAST) that contains multiple symbol table entries. However, an ...
Wang Haozhi's user avatar
-2 votes
1 answer
73 views

use method reset of unique_prt:free(): invalid pointer

When I use reset to rebind the unique_ptr, something bad happens but when use std::make_unique, everything is ok. int main() { unique_ptr<string> p= nullptr; auto l=[&p]()->void{ ...
Paoer111's user avatar
0 votes
2 answers
55 views

Polymorphic data types casting with objects wrapped within std::unique_ptr

I have a class hierarchy as follows #include <iostream> #include <memory> class A { public: virtual void Print() = 0; }; class B : public A { public: void Print() { ...
Aayush Anand's user avatar
0 votes
0 answers
125 views

Why aren't there memory leaks for std::unique_ptr initialized by new since C++17? [duplicate]

Let me quote part of the section in Professional C++ 5th. ed.(Page 237): Before C++17, you had to use make_unique() ... Consider the following call to a function called foo(): foo(unique_ptr<...
o_oTurtle's user avatar
  • 1,133
0 votes
1 answer
82 views

Overriding the deleter of unique_ptr

I have the following code in C++ #include <iostream> #include <memory> class MyType{ public: ~MyType() { std::cout<<"Destructor called"; } }; template<...
Vivek Mangal's user avatar
0 votes
1 answer
67 views

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 ...
Yash Sakariya's user avatar
0 votes
0 answers
150 views

Klocwork raises warning on make_unique

I have the following structure, struct MyStruct{ bool myBool, int myInt, std::unique_ptr<boost::circular_buffer<int>> myUniquePtr; } ; Let's say I construct the following object, MyStruct*...
floflo29's user avatar
  • 2,281
0 votes
1 answer
188 views

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 <...
Paul's user avatar
  • 6,225
1 vote
1 answer
187 views

Can't constuct a std::unique_ptr with a lambda as a custom deleter

I am trying to declare std::unique_ptr with a custom deleter: using FILE = int; FILE *f = nullptr; std::unique_ptr pf(f, [](FILE *f) {}); but without a success. What is wrong? How to use a lambda ...
Alexey Starinsky's user avatar
0 votes
1 answer
51 views

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 ...
doneThat's user avatar
0 votes
3 answers
189 views

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<IDBAbstractFactory> with ...
Rakesh Mehta's user avatar

1
2 3 4 5
45