shared-ptr Questions

3

EDIT: total re-edit because the original was becoming an unstructured mess :) Thanks for everyone's input so far; I hope I worked it into the text below. Question I'm in search for a lazily-creat...
Nedranedrah asked 17/9, 2015 at 21:29

2

How does an atomic<shared_ptr<>> work internally ? I'm not asking for the mechanics of the control block alongside with the stored data, which is easy to imagine for me, but for the ato...
Gassy asked 6/8, 2024 at 11:35

3

Solved

I'm implementing an A* pathfinding algorithm in C++ to solve a maze, but I'm encountering a segmentation fault (SIGSEGV) when the maze size is high (~10,000x10,000 or larger). The error occurs in ...

3

Solved

I have two classes A and B, where B is a subclass of A. I need both classes to use std::enable_shared_from_this. I tried this: #include <memory> #include <iostream> #include <vecto...

1

The proposal for shared_ptr<T[N]> has this paragraph: https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3920.html The unfortunate occurence of unique_ptr having lost its support for ...
Unquestioned asked 24/5, 2024 at 0:41

3

Solved

I'm using external networking library which returns some magic structures representing opened sockets and the docs say that when inserting them into STL containers, they should be compared using st...
Floodgate asked 13/7, 2015 at 7:58

20

Solved

I have this code that doesn't work, but I think the intent is clear: testmakeshared.cpp #include <memory> class A { public: static ::std::shared_ptr<A> create() { return ::std::ma...
Seftton asked 16/11, 2011 at 5:11

5

Solved

This question is about owning pointers, consuming pointers, smart pointers, vectors, and allocators. I am a little bit lost on my thoughts about code architecture. Furthermore, if this question ha...
Medawar asked 27/5, 2019 at 10:36

21

Solved

I have a solid understanding of most OOP theory but the one thing that confuses me a lot is virtual destructors. I thought that the destructor always gets called no matter what and for every object...
Oldfangled asked 20/1, 2009 at 12:58

2

Solved

In the book The C++ Standard Library at page 91 I have read this about shared_from_this(): The problem is that shared_ptr stores itself in a private member of Person’s base class, enable_shared...
Feola asked 10/8, 2015 at 16:14

5

Solved

Does C++11 standard library provide any utility to convert from a std::shared_ptr to std::unique_ptr, or vice versa? Is this safe operation?
Germaun asked 17/6, 2016 at 14:58

2

Solved

I'm storing a class (let's call it A) in an std::vector using C++ smart pointers (so the vector sigature is std::vector<std::shared_ptr<A>>). #include <iostream> #include <me...
Stand asked 12/12, 2017 at 12:2

1

Solved

Here is a C++ snippet. Func1 generates a shared object, which is directly moved into Func2. We think that there should not be overhead in Func3. Putting this snippet into Compiler Explorer, we see ...
Prospero asked 8/8, 2023 at 12:24

2

Solved

std::shared_ptr<Dog> pd; void F() { pd = std::make_shared<Dog>("Smokey"); } int main() { std::thread t1(F); std::thread t2(F); t1.join(); t2.join(); return 0; } std:...
Bowstring asked 30/6, 2023 at 0:25

5

Solved

I want to write some bytes to an array. To make use of modern C++ I have decided to use a smart pointer. #include <memory> #include <cstdint> using namespace std; void writeUint32_t(...
Ess asked 25/8, 2016 at 10:7

2

Solved

I was working on some C++ code using std::move on shared_ptr and got really weird output. I've simplified my code as below int func(std::shared_ptr<int>&& a) { return 0; } int main(...

4

Solved

What are the differences between shared pointers (such as boost::shared_ptr or the new std::shared_ptr) and garbage collection methods (such as those implemented in Java or C#)? The way I understan...
Unconscious asked 11/1, 2011 at 22:27

6

I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap. For example template <typename T> class MyExample { public: private...
Pearle asked 17/1, 2010 at 2:21

10

Solved

If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of whi...
Depone asked 15/3, 2015 at 9:58

13

Solved

shared_ptr is a reference counting smart pointer in the Boost library. The problem with reference counting is that it cannot dispose of cycles. I am wondering how one would go about solving this in...
Normalie asked 22/4, 2009 at 7:33

2

Why the compiler complains if the the thread function delaration is changed to void thr(std::shared_ptr<Base>& p).Complie error: gcc-10.1.0/include/c++/10.1.0/thread: In instantiation ...
Saltern asked 24/5, 2020 at 12:21

1

Seems like the obvious use of std::shared_ptr<T[]> (added with C++ 17) is to hold a pointer to T, and use delete[] when it's time to destroy the managed object. But this code: #include <m...
Foliar asked 19/1, 2020 at 19:1

5

Solved

Let's say I have a class which is a child class of enable_shared_from_this. The documentation of this base class says there should be a shared pointer which owns this class before calling shared_fr...
Nutbrown asked 10/12, 2015 at 22:43

6

Solved

I have the following shared_ptr to a map: std::shared_ptr<std::map<double, std::string>> and I would like to initialise it using braced-init. Is it possible? I've tried: std::strin...
Flagging asked 6/4, 2016 at 8:32

0

libc++ shared_ptr implementation release() for the sake of simplicity can be depicted as: void release() { if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) { delete this; } } Why do...
Selena asked 17/6, 2022 at 14:42

© 2022 - 2025 — McMap. All rights reserved.