shared-ptr

Why do std::shared_ptr<void> work

做~自己de王妃 提交于 2019-11-26 15:46:23
I found some code using std::shared_ptr to perform arbitrary cleanup at shutdown. At first I thought this code could not possibly work, but then I tried the following: #include <memory> #include <iostream> #include <vector> class test { public: test() { std::cout << "Test created" << std::endl; } ~test() { std::cout << "Test destroyed" << std::endl; } }; int main() { std::cout << "At begin of main.\ncreating std::vector<std::shared_ptr<void>>" << std::endl; std::vector<std::shared_ptr<void>> v; { std::cout << "Creating test" << std::endl; v.push_back( std::shared_ptr<test>( new test() ) ); std

How do shared pointers work?

浪子不回头ぞ 提交于 2019-11-26 15:35:39
问题 How do shared pointers know how many pointers point to that object? (shared_ptr, in this case) 回答1: Basically, shared_ptr has two pointers: a pointer to the shared object and a pointer to a struct containing two reference counts: one for "strong references," or references that have ownership, and one for "weak references," or references that don't have ownership. When you copy a shared_ptr , the copy constructor increments the strong reference count. When you destroy a shared_ptr , the

How is the std::tr1::shared_ptr implemented?

坚强是说给别人听的谎言 提交于 2019-11-26 15:19:49
问题 I've been thinking about using shared pointers, and I know how to implement one myself--Don't want to do it, so I'm trying std::tr1::shared_ptr ,and I have couple of questions... How is the reference counting implemented? Does it use a doubly linked list? (Btw, I've already googled, but I can't find anything reliable.) Are there any pitfalls for using the std::tr1::shared_ptr ? 回答1: shared_ptr must manage a reference counter and the carrying of a deleter functor that is deduced by the type of

What are potential dangers when using boost::shared_ptr?

折月煮酒 提交于 2019-11-26 15:19:03
问题 What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr? 回答1: Cyclic references: a shared_ptr<> to something that has a shared_ptr<> to the original object. You can use weak_ptr<> to break this cycle, of course. I add the following as an example of what I am talking about in the comments. class node : public enable_shared_from_this<node> { public : void set_parent(shared_ptr<node> parent)

What is the difference between an empty and a null std::shared_ptr in C++?

会有一股神秘感。 提交于 2019-11-26 15:17:15
问题 The cplusplus.com shared_ptr page calls out a distinction between an empty std::shared_ptr and a null shared_ptr . The cppreference.com page doesn't explicitly call out the distinction, but uses both "empty" and comparison to nullptr in its description of std::shared_ptr behavior. Is there a difference between an empty and a null shared_ptr ? Is there any use case for such mixed-behavior pointers? Does a non-empty null shared_ptr even make sense? Would there ever be a case in normal usage (i

shared_ptr: horrible speed

こ雲淡風輕ζ 提交于 2019-11-26 15:14:12
问题 When comparing two variants of pointers—classic vs. shared_ptr—I was surprised by a significant increase of the running speed of the program. For testing 2D Delaunay incremental Insertion algorithm has been used. Compiler settings: VS 2010 (release) /O2 /MD /GL, W7 Prof, CPU 3.GHZ DualCore Results: shared_ptr (C++ 0x00): N[points] t[sec] 100 000 6 200 000 11 300 000 16 900 000 36 Pointers: N[points] t[sec] 100 000 0,5 200 000 1 300 000 2 900 000 4 Running time of the shared_ptr versions is

shared_ptr and weak_ptr differences

风格不统一 提交于 2019-11-26 15:05:46
问题 I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting. This works well in preventing resource leaks in acyclic data structures, but if two or more objects contain tr1::shared_ptrs such that a cycle is formed, the cycle may keep each other's reference count above zero, even when all external pointers to

Why shared_from_this can&#39;t be used in constructor from technical standpoint?

送分小仙女□ 提交于 2019-11-26 14:33:56
问题 In the book The C++ Standard Library at page 91 I have read this about shared_from_this() : The problem is that shared_ptr stores itself in a private member of Person ’s base class, enable_shared_from_this<> , at the end of the construction of the Person. The relevant code snippet from the book is: class Person : public std::enable_shared_from_this<Person> { ... }; I don't understand two things here: who is this shared_ptr which stores itself? how he can store itself anywhere at the end of

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?

Using custom deleter with std::shared_ptr

扶醉桌前 提交于 2019-11-26 13:05:31
问题 I\'m trying to work out how to use std::shared_ptr with a custom deleter. Specifically, I\'m using it with SDL_Surface as: std::shared_ptr<SDL_Surface>(SDL_LoadBMP(....),SDL_FreeSurface); which compiles and runs fine. However, I would like to try out my own deleter and cannot work out how to do so. The documentation for SDL_FreeSurface is found here: http://sdl.beuc.net/sdl.wiki/SDL_FreeSurface in which I find the SDL_FreeSurface is declared as: void SDL_FreeSurface(SDL_Surface* surface); As