smart-pointers

Smart pointers. When, where and how?

别说谁变了你拦得住时间么 提交于 2019-12-05 21:43:17
First off, since there are different kinds of smart pointers, I'd like to focus this question on two of them: reference counted intrusive and non-intrusive smart pointers. The question is asked individualy for each pointer type. I am not really sure how to formulate my question, so here's what I'm not asking: I am not asking why, or when, are smart pointers needed. Neither which type of smart pointer should I use and for what. Here is what I'm asking, and I hope it's clear enough: When dealing with "smartly-managed" objects, in which contextes should I use which pointer semantics? That is,

Disadvantages of shared_ptr

耗尽温柔 提交于 2019-12-05 21:23:53
问题 With shared_ptr included in c++11, one could achieve a semi garbage-collected enviroment. Does the (inflationary?) usage come along with some disadvantages? I could imagine a class model, where you create a class in which you typedef your class at the end as a shared_ptr to abbreviate the syntax. ///////////////// //// MyClass //// ///////////////// #include <memory> class MyClass { public: Myclass(); }; typedef std::shared_ptr<MyClass> SharedMyClass; /////////////////////// //// Example

Is it possible to use SDL2 with smart pointers?

[亡魂溺海] 提交于 2019-12-05 21:22:23
I have this line of code //std::unique_ptr<SDL_Window> _window_; // this is somewhere else... _window_ = std::make_unique<SDL_Window>(SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, _WIDTH_, _HEIGHT_, SDL_WINDOW_SHOWN)); it produces the following compiler error In file included from /usr/include/c++/6/memory:81:0, from /home/user/prj/src/main.cpp:4: /usr/include/c++/6/bits/unique_ptr.h: In instantiation of ‘typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = SDL_Window; _Args = {SDL_Window*}; typename std::_MakeUniq<_Tp>::_

A new generic pointer any_ptr (now dumb_ptr) to make code more reusable among smart pointers

佐手、 提交于 2019-12-05 19:55:54
I have been using a lot of different boost smart pointers lately, as well as normal pointers. I have noticed that as you develop you tend to realise that you have to switch pointer types and memory management mechanism because you overlooks some circular dependency or some othe small annoying thing. When this happens and you change your pointer type you have to either go and change a whole bunch of you method signatures to take the new pointer type, or at each call site you have to convert between pointer types. You also have a problem if you have the same function but want it to take multiple

QCache and QSharedPointer

会有一股神秘感。 提交于 2019-12-05 18:29:42
The problem is, that I have a QVector of QSharedPointer and I want to put part of them to my QCache . How can I insert a shared pointer to QCache ? What happens if I set the pointer to my cache but destroy the QVector and the shared pointer in it? Will the memory be released and my cache points to nowhere? It is a bug if you put just a pointer to your item to QChache and at the same time such pointer is managed by QSharedPointer . The item object can be destroyed by QSharedPointer destructor, so QChache will have invalid pointer. It is a generic issue that you cannot have different owners of a

Smart pointers as class members for polymorphism

主宰稳场 提交于 2019-12-05 16:44:41
I'm new to smart pointers and I would be really grateful if somebody could give me a hint whether the way I'm handling smart pointers as class members is correct. More precisely, the solution that I would like to achieve is in the context of class polymorphism and should be ideally exception-safe. Given a container of heterogeneuous objects ( std::vector<shared_ptr<CBase> > my_vector ), the usual way to add elements is: my_vector.push_back( shared_ptr<CBase>(new CChild(1))) , so that later on, one can call the member function of the specific derived class by doing: my_vector[0]->doSomething()

Is there a CUDA smart pointer?

你说的曾经没有我的故事 提交于 2019-12-05 14:53:47
If not, what is the standard way to free up cudaMalloc ed memory when an exception is thrown? (Note that I am unable to use Thrust.) You can use RAII idiom and put your cudaMalloc() and cudaFree() calls to the constructor and destructor of your object respectively. Once the exception is thrown your destructor will be called which will free the allocated memory. If you wrap this object into a smart-pointer (or make it behave like a pointer) you will get your CUDA smart-pointer. 来源: https://stackoverflow.com/questions/16509414/is-there-a-cuda-smart-pointer

C++ “smart pointer” template that auto-converts to bare pointer but can't be explicitly deleted

不想你离开。 提交于 2019-12-05 10:37:29
I am working in a very large legacy C++ code base which shall remain nameless. Being a legacy code base, it passes raw pointers around all over the place. But we are gradually trying to modernize it and so there are some smart pointer templates as well. These smart pointers (unlike, say, Boost's scoped_ptr) have an implicit conversion to the raw pointer, so that you can pass one of them into a routine that takes a raw pointer without having to write .get() . A big downside of this is that you can also accidentally use one in a delete statement, and then you have a double free bug, which can be

How can unique_ptr have no overhead if it needs to store the deleter?

隐身守侯 提交于 2019-12-05 08:53:53
问题 First take a look at what C++ Primer said about unique_ptr and shared_ptr : $16.1.6. Efficiency and Flexibility We can be certain that shared_ptr does not hold the deleter as a direct member, because the type of the deleter isn’t known until run time. Because the type of the deleter is part of the type of a unique_ptr , the type of the deleter member is known at compile time. The deleter can be stored directly in each unique_ptr object. So it seems like that the shared_ptr does not have a

Best way to delete a std::unique_ptr from a vector with a raw pointer?

一笑奈何 提交于 2019-12-05 06:42:42
So I have a vector like so: std::vector<std::unique_ptr<SomeClass>> myVector; Then I have another vector which contains raw pointers of SomeClass : std::vector<SomeClass*> myOtherVector; If there is an element inside myOtherVector it will also be inside myVector , so I want to go through each element in myOtherVector and remove the same element from myVector . Then clear out the vector. This is what I came up with: for(size_t i = 0; i < myOtherVector.size(); i++) { myVector.erase(std::remove(myVector.begin(), myVector.end(), myOtherVector[i]), myVector.end()); } myOtherVector.clear(); This