3,403 questions
-3
votes
1
answer
132
views
Linkedlist create like below with shared_ptr is proper or wrong - [closed]
I am trying to create Linkedlist, where array is passed, and linkedlist is returned. However, there seems something wrong, and the linkedlist returned is circular.
/**
* Shared pointer, pass vector ...
-5
votes
3
answers
209
views
std::shared_ptr const vs non-const, instance vs reference in constructor
Consider passing a shared pointer of const std::shared_ptr<Object> to the ctor of following class.
struct MyClass {
explicit MyCLass(const std::shared_ptr<const Object> & input) : ...
2
votes
1
answer
197
views
Move value out of shared_ptr or copy if it is shared
Context
We store triangular mesh data in large buffers as Buffer = std::vector<char>. These buffers can easily reach 1 GB in size.
We also share these buffers as SharedBuffer = std::shared_ptr&...
0
votes
1
answer
182
views
How does the implementation of C++ std::weak_ptr upgrade to std::shared_ptr and access the object without risking a use-after-free?
How does the implementation of C++ std::weak_ptr upgrade to std::shared_ptr and access the object without risking a use-after-free?
The scenario: In a concurrent setting, you have a weak pointer to an ...
4
votes
1
answer
134
views
How to create a std::shared_ptr with the same control block as an expired std::weak_ptr
TL;DR How can a std::shared_ptr be created from an expired std::weak_ptr so that it uses the same control block as the std::weak_ptr?
I ran the following test case, and noticed two oddities:
#include ...
2
votes
1
answer
81
views
Why is the output of id() applied to a Python object returned from C++ via pybind11 unstable?
I am using a holder class to store a std::shared_ptr to another C++ class created in Python thus:
#include <memory>
#include <iostream>
#include <pybind11/pybind11.h>
class Child {
...
5
votes
0
answers
250
views
Am I over-encouraging people to use shared_ptr's? [closed]
In this earlier SO question:
What is a smart pointer and when should I use one?
Ten years ago, I gave a relative simple answer and got a bunch of upvotes. One paragraph in my answer reads:
Use std::...
-1
votes
3
answers
183
views
Why, if constructing a shared_ptr from raw pointer, "main()" exits with some negative error code? [duplicate]
Just experimenting with shared_ptr to explore its functionalities.
I'm constructing a new shared_ptr object using a raw pointer from other shared_ptr object.
The following code is compiling and runs ...
0
votes
3
answers
180
views
Custom shared_ptr when reaching count 1
I have a following issue in C++.
I am creating an object factory, which has a method returning shared_ptr to an object of class.
The object factory uses a buffer of pre-allocated objects called "...
1
vote
4
answers
141
views
Count of a shared_ptr when managing the same object
If a shared_ptr is declared twice managing the same object, why does the count not increase to two, since the same object is being managed?
int i = 1;
std::shared_ptr<int> p1 = std::...
1
vote
1
answer
144
views
`std::shared_ptr` vs `std::optional` for thread safe queue
In "C++ Concurrency in action" from Anthony Williams (2012) there is a thread safe queue implemented by storing std::shared_ptr<T> like so
template<typename T>
class ...
3
votes
0
answers
134
views
How to store std::shared_ptr in std::vector in thread-safe way? [closed]
I have many threads which share access to the same pool of objects, and some threads could remove objects, so I use std::vector<std::shared_ptr<T>> to hold pointers to the objects so that ...
10
votes
5
answers
686
views
How can I transparently process std::vector of T and std::vector of std::shared_ptr<T> in a template?
I want to apply the same template algorithm to std::vectors which contain objects of some type T and (different) std::vectors which contain std::shared_ptrs to objects of some type T.
Can I ...
-3
votes
2
answers
168
views
Are smart pointers that are members of a class still automatically freed? [closed]
I am developing a game in C++, and have been using vectors of smart pointers to store some level data. The game runs and works without issue, but I was wondering whether or not the memory held by the ...
8
votes
0
answers
203
views
Why is std::atomic_ref<std::shared_ptr<T>> unavailable?
Prior to C++20 a std::shared_ptr<T> could be accessed without synchronization via its methods, or atomically using std::atomic_... functions.
In C++20, these functions are deprecated, and ...