smart-pointers

C++11: Replace all non-owning raw pointers with std::shared_ptr()?

回眸只為那壹抹淺笑 提交于 2019-11-26 15:11:12
问题 With the advent of std::unique_ptr , the blemished std::auto_ptr can finally be put to rest. So for the last several days, I have been changing my code to use smart pointers and to eliminate all delete from my code. Although valgrind says my code is memory-clean, the semantic richness of smart pointers will make for cleaner and easier-to-understand code. In most of the code, the translation is simple: use std::unique_ptr for in place of the raw pointers held by the owning objects, throw out

How to avoid memory leak with shared_ptr?

假装没事ソ 提交于 2019-11-26 13:07:07
问题 Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout << \"~A\" << std::endl; } shared_ptr<B> b; }; struct B { ~B() { std::cout << \"~B\" << std::endl; } shared_ptr<A> a; }; int main() { shared_ptr<A> a (new A); shared_ptr<B> b (new B); a->b = b; b->a = a; return 0; } There is no output . No desctructor is called. Memory leak. I have always believed that the smart pointer helps avoid memory leaks. What should I do if I need cross-references in the classes?

Where is shared_ptr?

荒凉一梦 提交于 2019-11-26 12:37:37
问题 I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr (and working). Simply stating std , tr1 and <memory> is not helping at all! I have downloaded boosts and all but still it doesn\'t show up! Can someone help me by telling exactly where to find it? Thanks for letting me vent my frustrations! EDIT: I see my title has been changed. Sorry about that. So... it was also

How can I use covariant return types with smart pointers?

点点圈 提交于 2019-11-26 12:27:21
问题 I have code like this: class RetInterface {...} class Ret1: public RetInterface {...} class AInterface { public: virtual boost::shared_ptr<RetInterface> get_r() const = 0; ... }; class A1: public AInterface { public: boost::shared_ptr<Ret1> get_r() const {...} ... }; This code does not compile. In visual studio it raises C2555: overriding virtual function return type differs and is not covariant If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is

smart pointers and arrays

微笑、不失礼 提交于 2019-11-26 12:24:54
问题 How do smart pointers handle arrays? For example, void function(void) { std::unique_ptr<int> my_array(new int[5]); } When my_array goes out of scope and gets destructed, does the entire integer array get re-claimed? Is only the first element of the array reclaimed? Or is there something else going on (such as undefined behavior)? 回答1: It will call delete[] and hence the entire array will be reclaimed but I believe you need to indicate that you are using an array form of unique_ptr by: std:

When should I use raw pointers over smart pointers?

人盡茶涼 提交于 2019-11-26 12:18:20
问题 After reading this answer, it looks like it is a best practice to use smart pointers as much as possible, and to reduce the usage of \"normal\"/raw pointers to minimum. Is that true? 回答1: No, it's not true. If a function needs a pointer and has nothing to do with ownership, then I strongly believe that a regular pointer should be passed for the following reasons: No ownership, therefore you don't know what kind of a smart pointer to pass If you pass a specific pointer, like shared_ptr , then

Is auto_ptr deprecated?

ε祈祈猫儿з 提交于 2019-11-26 12:08:46
问题 Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership transfer instead of shared_ptr? If unique_ptr is not in the standard, then do I need to use shared_ptr instead? 回答1: UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr has been deprecated. The advice is entirely valid. In C++0x std::auto_ptr will be deprecated in favor of std::unique_ptr . The choice of smart pointer will depend on your use case and your requirements, with std:

Passing shared_ptr<Derived> as shared_ptr<Base>

喜你入骨 提交于 2019-11-26 11:55:14
问题 What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type? I generally pass shared_ptr s by reference to avoid a needless copy: int foo(const shared_ptr<bar>& ptr); but this doesn\'t work if I try to do something like int foo(const shared_ptr<Base>& ptr); ... shared_ptr<Derived> bar = make_shared<Derived>(); foo(bar); I could use foo(dynamic_pointer_cast<Base, Derived>(bar)); but this seems sub-optimal for two reasons: A

Why is auto_ptr being deprecated?

ε祈祈猫儿з 提交于 2019-11-26 11:55:02
问题 I heard auto_ptr is being deprecated in C++11. What is the reason for this? Also I would like to know the difference between auto_ptr and shared_ptr . 回答1: The direct replacement for auto_ptr (or the closest thing to one anyway) is unique_ptr. As far as the "problem" goes, it's pretty simple: auto_ptr transfers ownership when it's assigned. unique_ptr also transfers ownership, but thanks to codification of move semantics and the magic of rvalue references, it can do so considerably more

Smart Pointers: Or who owns you baby? [closed]

人盡茶涼 提交于 2019-11-26 11:27:34
C++ is all about memory ownership Aka " Ownership Semantics " It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented by the type a RAW pointer is wrapped inside thus in a good (IMO) C++ program it is very rare [RARE not NEVER] to see RAW pointers passed around (as RAW pointers have no inferred ownership thus we can not tell who owns the memory and thus without careful reading of the documentation you can't tell who is responsible for ownership). Conversely it is