smart-pointers

Smart pointers. When, where and how?

℡╲_俬逩灬. 提交于 2019-12-07 14:21:27
问题 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

Smart pointers with addrinfo struct

帅比萌擦擦* 提交于 2019-12-07 12:25:29
问题 I need to deal with two struct addrinfo pointers. Since I'm coding in C++(11), I've to make my code exception-safe. Indeed, my costructors may throw a runtime_error . When you don't need that kind of struct anymore, you should call freeaddrinfo in order to free the list inside the struct. Please consider the following code: #include <memory> #include <netdb.h> class SomeOtherClass { public: SomeOtherClass() : hints(new addrinfo), result(new addrinfo) { /*stuff*/ } ~SomeOtherClass() {

How to include only BOOST smart pointer codes into a project?

泪湿孤枕 提交于 2019-12-07 07:58:26
问题 What are best practices to include boost smart pointer library only without adding all boost libraries into the project? I only want boost smart pointer library in my project and I don't want to check in/commit 200 MB source codes (boost 1.42.0) into my project repository just for that. What more, my windows mobile project itself doesn't even reach 10% of that size! 回答1: For just the smart pointer library, you have two options. Copy the headers you include in your source files ( shared_ptr

Is there a CUDA smart pointer?

筅森魡賤 提交于 2019-12-07 07:54:58
问题 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.) 回答1: 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:

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

一个人想着一个人 提交于 2019-12-07 03:46:33
问题 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

Advantages/disadvantages of auto pointers

风流意气都作罢 提交于 2019-12-07 03:36:00
问题 What are the advantages and disadvantages of using auto pointers (auto_ptr), compared to ordinary pointers? I've heard it does automatic releasing of memory but how come it is not used often? 回答1: The main drawback of std::auto_ptr is that it has the transfer-of-ownership semantic. That makes it impossible to store std::auto_ptr in STL containers because the containers use the copy constructor when you store or get an element. Also, another important aspect that i have noticed about the std:

Static method to create an object instead of constructor

半世苍凉 提交于 2019-12-07 01:22:51
问题 I'm creating a GUI in my C++ application and I have a class called GUIObject which is base class for all other components, like for example Button , CheckBox , Window etc. I also have a class GUIObjectsStorage , which consists of all GUIObject s that are created. So far I've been working with raw pointers, so I just had this constructor for GUIObject class: GUIObject::GUIObject() : { GUIObjectsStorage::Instance().addObject(this); } And it was ok for my needs, because whenever I wanted to

Wrap C allocation for RAII

安稳与你 提交于 2019-12-07 01:00:51
问题 I've these plain C functions from a library: struct SAlloc; SAlloc *new_salloc(); void free_salloc(SAlloc *s); Is there any way I can wrap this in C++ to a smart pointer (std::unique_ptr), or otherwise a RAII wrapper ? I'm mainly curious about the possibilities of the standard library without creating my own wrapper/class. 回答1: Yes, you can reuse unique_ptr for this. Just make a custom deleter. struct salloc_deleter { void operator()(SAlloc* s) const { free_salloc(s); // what the heck is the

Array of polymorphic objects

僤鯓⒐⒋嵵緔 提交于 2019-12-06 19:23:51
问题 I commonly come across the need to create arrays or vectors of polymorphic objects. I'd usually prefer to use references, rather than smart pointers, to the base class because they tend to be simpler. Arrays and vectors are forbidden from containing raw references, and so I've tended to use smart pointers to the base classes instead. However, there is also the option to use std::reference_wrapper instead: https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper From what I can

Why can't a pointer be automatically converted into a unique_ptr when returning it?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 17:26:23
问题 Let me pose my question through an example. #include <memory> std::unique_ptr<int> get_it() { auto p = new int; return p; } int main() { auto up ( get_it() ); return 0; } This fails to compile with the following error: a.cpp:5:9: error: could not convert ‘p’ from ‘int*’ to ‘std::unique_ptr<int>’ return p; ^ Why isn't there an automatic conversion from a raw pointer to a unique one here? And what should I be doing instead? Motivation: I understand it's supposed to be good practice to use smart