smart-pointers

Should boost::ptr_vector be used in place std::vector all of the time?

别来无恙 提交于 2019-12-03 14:18:43
Just a conceptual question that I've been running into. In my current project it feels like I am over-using the boost smart_ptr and ptr_container libraries. I was creating boost::ptr_vectors in many different objects and calling the transfer() method to move certain pointers from one boost::ptr_vector to another. It is my understanding that it is important to clearly show ownership of heap allocated objects. My question is, would it be desirable to use these boost libraries to create heap-allocated members that belong to an object but then use normal pointers to these members via get() when

Replacing auto_ptr in VC++ 8

人走茶凉 提交于 2019-12-03 12:35:40
std::auto_ptr is broken in VC++ 8 (which is what we use at work). My main gripe with it is that it allows auto_ptr<T> x = new T(); , which of course leads to horrible crashes, while being simple to do by mistake. From an answer to another question here on stackoverflow: Note that the implementation of std::auto_ptr in Visual Studio 2005 is horribly broken. http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98871 http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842 I want to use boost::scoped_ptr , for pointers that shouldn't pass

unique_ptr heap and stack allocation

妖精的绣舞 提交于 2019-12-03 12:24:01
问题 Raw pointers can point to objects allocated on the stack or on the heap. Heap allocation example: // heap allocation int* rawPtr = new int(100); std::cout << *rawPtr << std::endl; // 100 Stack allocation example: int i = 100; int* rawPtr = &i; std::cout << *rawPtr << std::endl; // 100 Heap allocation using auto_ptr example: int* rawPtr = new int(100); std::unique_ptr<int> uPtr(rawPtr); std::cout << *uPtr << std::endl; // 100 Stack allocation using auto_ptr example: int i = 100; int* rawPtr =

using custom deleter with unique_ptr

时光毁灭记忆、已成空白 提交于 2019-12-03 11:57:44
With shared_ptr you can use a custom deleter, like: auto fp = shared_ptr<FILE>( fopen("file.txt", "rt"), &fclose ); fprintf( fp.get(), "hello\n" ); and this will remember to fclose the file regardless of how the function exits. However, it seems a bit overkill to refcount a local variable, so I want to use unique_ptr : auto fp = unique_ptr<FILE>( fopen("file.txt", "rt"), &fclose ); however, that does not compile. Is this a defect? Is there a simple workaround? Im I missing something trivial? Should be unique_ptr<FILE, int(*)(FILE*)>(fopen("file.txt", "rt"), &fclose); since http://en

Feasibility of automatic cycle breaker for `std::shared_ptr`

柔情痞子 提交于 2019-12-03 11:08:29
C++11 introduced reference-counted smart pointers, std::shared_ptr . Being reference counted, these pointers are unable to automatically reclaim cyclic data structures. However, automatic collection of reference cycles was shown to be possible, for example by Python and PHP . To distinguish this technique from garbage collection, the rest of the question will refer to it as cycle breaking . Given that there seem to be no proposals to add equivalent functionality to C++, is there a fundamental reason why a cycle breaker similar to the ones already deployed in other languages wouldn't work for

STL class for reference-counted pointers?

我们两清 提交于 2019-12-03 11:06:46
This should be trivial but I can't seem to find it (unless no such class exists!) What's the STL class (or set of classes) for smart pointers? UPDATE Thanks for the responses, I must say I'm surprised there's no standard implementation. I ended up using this one: http://archive.gamedev.net/reference/articles/article1060.asp With the exception of the already mentionned TR1 shared_ptr, there is no reference-counted pointer in STL. I suggest you use boost::shared_ptr (downloading boost will be enough, there is nothing to compile, its implementation is header-only). You may also want to have a

unique_ptr and polymorphism

て烟熏妆下的殇ゞ 提交于 2019-12-03 10:40:25
I have some code that currently uses raw pointers, and I want to change to smart pointers. This helps cleanup the code in various ways. Anyway, I have factory methods that return objects and its the caller's responsibility to manager them. Ownership isn't shared and so I figure unique_ptr would be suitable. The objects I return generally all derive from a single base class, Object . For example, class Object { ... }; class Number : public Object { ... }; class String : public Object { ... }; std::unique_ptr<Number> State::NewNumber(double value) { return std::unique_ptr<Number>(new Number(this

creating a shared_ptr from unique_ptr

末鹿安然 提交于 2019-12-03 10:27:05
问题 In a piece of code I reviewed lately, which compiled fine with g++-4.6 , I encountered a strange try to create a std::shared_ptr from std::unique_ptr : std::unique_ptr<Foo> foo... std::make_shared<Foo>(std::move(foo)); This seems rather odd to me. This should be std::shared_ptr<Foo>(std::move(foo)); afaik, though I'm not perfectly familiar with moves (and I know std::move is only a cast, nothing get's moved). Checking with different compilers on this SSC(NUC*)E #include <memory> int main() {

enable_shared_from_this and inheritance

南笙酒味 提交于 2019-12-03 09:31:17
I've got a type which inherits from enable_shared_from_this<type> , and another type that inherits from this type. Now I can't use the shared_from_this method because it returns the base type and in a specific derived class method I need the derived type. Is it valid to just construct a shared_ptr from this directly? Edit: In a related question, how can I move from an rvalue of type shared_ptr<base> to a type of shared_ptr<derived> ? I used dynamic_cast to verify that it really was the correct type, but now I can't seem to accomplish the actual move. Once you obtain the shared_ptr<Base> , you

Should I use std::shared pointer to pass a pointer?

拈花ヽ惹草 提交于 2019-12-03 08:32:24
问题 Suppose I have an object which is managed by an std::unique_ptr . Other parts of my code need to access this object. What is the right solution to pass the pointer? Should I just pass the plain pointer by std::unique_ptr::get or should I use and pass an std::shared_ptr instead of the std::unique_ptr at all? I have some preference for the std::unique_ptr because the owner of that pointer is actually responsible for cleanup. If I use a shared pointer, there's a chance that the object will