2,870 questions
3
votes
1
answer
130
views
Why can’t I efficiently move-construct `std::polymorphic<Base>` from `std::polymorphic<Derived>`?
As I understand it, std::polymorphic<T> and std::indirect<T> are const-propagating wrapper types for an owning pointer to a T object, where the target of the pointer is copied with the ...
-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 ...
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 ...
1
vote
1
answer
148
views
How to handle return of unique_ptr in trompeloeil RETURN expectations
Hope this message finds you well.
I'm a little bit lost while using trompeloeil expectations syntax.
Let's say I have this:
#include <memory>
class IA {
public:
virtual ~IA() = default;
...
3
votes
1
answer
176
views
Smart pointers and polymorphism - Use in a model class is causing casting issues
I'm attempting to make a game in C++ where pieces will move around a gameboard.
In our team, we're trying to be good and use as much modern methodology as we can, given we've gotten rusty and now have ...
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::...
0
votes
2
answers
311
views
Will unique pointer free the memory? [duplicate]
Let's say you allocate an object and pass its address in a Windows message (posted, not sent). In the message handler, the address is recovered and used to initialize a std::unique_ptr. Will the ...
2
votes
4
answers
315
views
std::vector: when is a pointer not a pointer?
I have this existing working code, which I am trying to convert to implementation:
Bitmap *pbitmap = new Bitmap(L"images.png");
nWidth = pbitmap->GetWidth();
nHeight = pbitmap->...
1
vote
2
answers
100
views
invalid user-defined conversion from Pointer<Node> to Node*
#include <cstddef>
#include <stdexcept>
#include <cassert>
template <typename T>
class Pointer {
private:
std::ptrdiff_t offset;
std::ptrdiff_t calculateOffset(const ...
3
votes
1
answer
126
views
Can an object that owns a unique pointer be unique pointed to?
I have the following minimal reproducible example where it seems that my unique-pointer-containing object B is not allowed to contain a unique pointer to another instance of B:
#include <iostream&...
1
vote
0
answers
46
views
Atomic implementation of intrusive shared pointers allowing construction from another being destroyed
Intrusive shared pointers have some advantages over classical control block shared pointers, as they allow, as far as I understand, atomic, lock-free concurrent operations with single writer and ...
1
vote
1
answer
152
views
Smart pointers vs raw pointers for raytracer [closed]
I wrote a toy raytracer following the popular raytracing in one weekend series that uses heavily shared_ptr for more or less everything. The codebase in nice and tidy but extremely slow (a 400x400 ...
3
votes
1
answer
140
views
How to prevent code heavily relying on polymorphism from being littered with `make_shared` all over the place?
I am writing a UI framework for a microcontroller, in which I want to do a hierarchical menu system.
For that in particular I have the following classes:
class MenuNode {
public:
MenuNode(const ...
2
votes
1
answer
148
views
Why doesn't std::unique_ptr allow polymorphic conversion for reference-type deleters?
I was experimenting with std::unique_ptr and reference-type deleters, and noticed that it does not allow polymorphic conversion when moving a unique_ptr with a derived-class deleter into one expecting ...
3
votes
3
answers
260
views
How does Qt manages memory with smart pointers ? My app should be crashing
I hope this question will find you well. I'm a little puzzled on this one. I really don't understand how Qt Object model tree handles memory with smart pointers.
I created this Qt5 basic test app just ...
4
votes
3
answers
139
views
Reference counted singleton
I have have found the following code in my company's codebase. The logic behind is to free the config singleton when nobody is using it, as some users have been found to inflate its size, which has ...
-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 ...
3
votes
1
answer
76
views
Generic Trait Parameters in Rust
I have a custom smart pointer which I'm trying to extend to trait objects. Essentially what I'm trying to make is the following:
use std::ptr::NonNull;
struct Ptr<T: ?Sized> {
data: NonNull&...
3
votes
2
answers
135
views
std::move captured unique_ptr inside lambda
I wanted to capture std::unique_ptr into lambda and then move it into another function inside lambda, but my two minimal examples fail to compile and I'm not sure why.
Can somebody explain to me, why ...
1
vote
0
answers
50
views
A question about the smart pointer and deleter in C++ Boost Lib
I created a smart Pointer using Boost Lib :
boost::shared_array<int> spArray = nullptr;
Then, I let it manage a 16bit aligned memory:
spArray.reset((int *)memalign(16, 100 * sizeof(int)));
The ...
0
votes
0
answers
61
views
How to deal with different reference & smart-pointer types [duplicate]
I'm a bit confused about how to handle the different reference and smart pointer types in this example:
use std::rc::Rc;
use std::sync::Arc;
struct Data{}
trait ApiClient {
fn load_data() -> ...
0
votes
0
answers
54
views
Using SWIG to wrap C++ pure virtual class as director to Java
Brief
I have a C++ library that provides sort of Sender and Listener functionality.
The Listener class is pure virtual class that is intended to be extended by the client.
The Sender class accepts ...
1
vote
2
answers
115
views
Using unique_ptr and custom deleter with the fftw3 library
When using the fftw3 fast fourier transform (FFT) library, a plan variable is declared and initialised, it is then used to perform some FFTs and subsequently destroyed when the memory is to be freed:
#...
0
votes
3
answers
166
views
How to put std::function in a smart pointer like std::unique_ptr and use it? [closed]
How to put std::function in a smart pointer like std::unique_ptr and use it?
I have already tried but it didn't work.
Here is the code that I tried already:
const inline void PrintSection(TurboINI::...
2
votes
2
answers
135
views
What are the benefits of using two containers of shared_ptr over one of unique_ptr and the other of raw pointers?
I'm working on a game, represented by the Game class, which owns every GameEntity and stores them in a container.
However, for convenience my Game class would also like to have a dedicated container ...
2
votes
4
answers
484
views
How to make shared pointer from raw pointer and make other shared pointers aware of it?
I have some hierarchy (through composition, not inheritance) of classes: Child and Parent. Child can have multiple parents, also Parent could do the same. I want the Child class lifetime being managed ...
0
votes
1
answer
119
views
How can I use/modify a class in different functions of another class?
I'm trying to create a game in C++, using SDL.
My structure for the game-code in the main is like this:
Create object game. (create NEEDED objects such as the SDL window or camera)
Call function ...
0
votes
1
answer
167
views
C++ smart pointers to FFmpeg objects
Can I create and use C++ smart pointers for different pointer types from FFmpeg?
"AVCodecContext *" which is used only as a pointer in all functions except deallocation.
Alloc:
...
4
votes
1
answer
197
views
Why weak_ptr::use_count may return a different count to shared_ptr::use_count?
The cppreference.com's entry on weak_ptr::use_count includes the caveat:
The usage and behavior of this function are similar to std::shared_ptr::use_count, but it returns a different count.
Since it'...
1
vote
1
answer
260
views
httplib send content with the content provider and smart pointer
I want to use the c++ httplib feature of the server to send a content with a content provider.
But I want to use it with a smart pointer because of previous calculation doing before the dispatch.
If I ...
-1
votes
1
answer
160
views
Problem with std::unique_ptr and dangling pointer [duplicate]
When I run this code:
#include <memory>
#include <iostream>
class A {
public:
virtual std::unique_ptr<A> clone() = 0;
};
class B : public A {
private:
int b0;
public:
B(const B&...
0
votes
2
answers
84
views
Is it possible that before creating a copy of shared_ptr the original shared_ptr goes out of scope
With below code snippet is it ever possible that output will print "Shared Ptr out of scope"?
What I am trying is before a shared copy is made the orginal shard_ptr goes out of scope. I am ...
2
votes
1
answer
125
views
Unexpected Order of Destructor Calls with std::shared_ptr and std::weak_ptr in C++
Question:
I have a C++ program using std::shared_ptr and std::weak_ptr involving two classes, A and B. The program creates shared pointers for both classes and sets up a relationship between them, ...
-2
votes
1
answer
105
views
A unique_ptr defined as base class then cast in as derived class [duplicate]
I would like to see two object printing from two different derived classes, and the key is to cast the object to derived class after being define as base. Here is the code:
#include <iostream>
#...
0
votes
1
answer
85
views
Getting object with a unique_ptr member from a std::map()
I am trying to get a class object (with a unique_ptr member) from a std::map (previously filled), and error occurs. Here is the code:
classes.hpp
#ifndef CLASSMETHODNOTCALLED_CLASSES_HPP
#define ...
0
votes
1
answer
112
views
How to return a unique_ptr reference to a const type
I'd like a member function to return a unique_ptr reference to a const type, just like in raw pointers.
Here is what I mean:
class Foo {
private:
int _number;
public:
[[nodiscard]] ...
0
votes
1
answer
126
views
segfaults in C++ when using std::shared_ptr
In the code bellow, when using std::shared_ptr I get a segmentation fault. However, when using normal pointers, the same problem doesn't occur.
In summary, I want to define a ConstrainedVariable ...
5
votes
3
answers
117
views
SIGSEGV when trying to solve large mazes with A*
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 the ...
1
vote
1
answer
87
views
Which smart pointer to insert into linked list to maintain polymorphism?
I'm writing a simple compiler and I have a base class:
class Instruction
{
public:
virtual std::string get_name() const = 0;
};
I create an instruction such as Compute:
class Compute
: public ...
2
votes
2
answers
180
views
pushing a unique_ptr into a vector of variant unique_ptr
I am playing around with C++ and faced this problem.
If I have a variant that looks like this:
using Fruit = variant<Apple, Tomato>
and a vector of unique_ptr of Fruit:
vector<unique_ptr<...
-1
votes
1
answer
89
views
Is returning a reference to a managed memory a malpractice?
Given this code:
class Clazz {
private:
std::shared_ptr<int> something;
public:
Clazz() : something(std::make_shared<int>(0)) {}
int &getSomething() {
return *something;
...
2
votes
2
answers
146
views
How to return smart pointers and covariance in C++
I am following this tutorial to understand how to return smart pointers and covariance in C++.
#include <memory>
#include <iostream>
class cloneable
{
public:
virtual ~...
2
votes
3
answers
290
views
Why does the Rust compiler drop unused variables in the reverse order they were declared?
In the Rust book an example is provided in chapter 15 section 3 to show when Rust runs the drop function on the variables c and d.
struct CustomSmartPointer {
data: String,
}
impl Drop for ...
0
votes
1
answer
198
views
Build error of "No matching constructor for initialization" when initialize share_ptr<T[]> from unique_ptr<T[]>
First, I read this answer to realize why we want to convert an unique_ptr to a shared_ptr sometimes. Then, I follow the instructions of the converting constructor on cppreference shared_ptr.
In C++11 ...
0
votes
1
answer
76
views
How to Implement operator-> for a Custom UniquePtr Class in C++? [duplicate]
I am trying to write my own implementation of unique_ptr in C++, and here is my UniquePtr header file:
#include <iostream>
template <typename T>
class UniquePtr
{
T* ptr;
public:
...
1
vote
0
answers
154
views
C++ std::weak_ptr::lock() seems to be executed at the same time as the destructor
I have a scenario where I need to use weak_ptr to cache resource, and use shared_ptr externally to determine the life cycle of the resource. When applying for this resource, I must ensure that it was ...
0
votes
0
answers
106
views
Exception when handling std::shared_ptr and using std::dynamic_pointer_cast
I currently have this problem with a std::shared_ptr. Or at least, I think the problem is there.
I have an app whose input data are passed through an interface from the calling program to the app ...
2
votes
2
answers
100
views
Strange behavior when passing a cable captures `std::initializer_list<std::shared_ptr<Conversation>>` to the complete handler
The code snippet below always closes the connection at once when a client has connected to the server, which is really out of my expectation.
#include <iostream>
#include <memory>
#include ...
0
votes
1
answer
64
views
Data race in the code snippet mentioned in other SO which read/write could not be invoked in the same time
The code below is seen at this SO except the comment in the original question has been removed.
std::weak_ptr<int> g_w;
void f3()
{
std::shared_ptr<int>l_s3 = g_w.lock(); //2. here ...
2
votes
1
answer
116
views
How std::weak_ptr::lock() is implemented to garantee both shared_ptr and weak_ptr can be used from threads without further synchronization
How std::weak_ptr::lock() is implemented to garantee both shared_ptr and weak_ptr can be used from threads without further synchronization(see the example code).
As per the cppreference, which says ...