smart-pointers

C++ 11: smart pointers usage [duplicate]

≡放荡痞女 提交于 2020-01-22 03:46:15
问题 This question already has answers here : Which kind of pointer do I use when? (4 answers) Closed 5 years ago . What are the best practices for using smart. Are there situations in which i should prefer using raw pointer instead of smart pointers? For example, if i know that class A creates class B and is the only owner of B - if there a reason to use smart pointers? If you know of any good articles on that subject, please share. 回答1: If the pointer owns the object at any time, then use a

why std::unique_ptr vector gets invalid pointer exception

强颜欢笑 提交于 2020-01-21 19:16:27
问题 I wrote simple code to help me understand smart pointers: string s = "str"; vector <unique_ptr<string>> pv ; pv.push_back(unique_ptr<string>(&s)); cout<<*(pv[0])<<endl; This code compiles fine, but gets me a runtime error: str * Error in `...': munmap_chunk(): invalid pointer: 0x00007ffd956e57e0 * Aborted (core dumped) What happened and what have I done wrong? 回答1: In the std::unique_ptr 's destructor it will call delete on the &s pointer which was not allocated via new . Just use: std:

why std::unique_ptr vector gets invalid pointer exception

淺唱寂寞╮ 提交于 2020-01-21 19:16:08
问题 I wrote simple code to help me understand smart pointers: string s = "str"; vector <unique_ptr<string>> pv ; pv.push_back(unique_ptr<string>(&s)); cout<<*(pv[0])<<endl; This code compiles fine, but gets me a runtime error: str * Error in `...': munmap_chunk(): invalid pointer: 0x00007ffd956e57e0 * Aborted (core dumped) What happened and what have I done wrong? 回答1: In the std::unique_ptr 's destructor it will call delete on the &s pointer which was not allocated via new . Just use: std:

How to pass shared_ptr to class with lower lifetime?

别来无恙 提交于 2020-01-21 08:49:25
问题 I'd like to optimize my code. I have one class that have shared_ptr data member. In some method of this class I create objects that need use this member (just to get information from object pointed by shared_ptr). I know that lifetime of these created objects is lower than my main class. How to pass this pointer? I think another shared_ptrs are unnecessary (because I have warranty that object will exist). So what should get my created classes? Should they get raw pointer? Weak_ptr? Or the

Using QSharedPointer with new[] yields “Mismatched free() / delete / delete[]” in valgrind

◇◆丶佛笑我妖孽 提交于 2020-01-20 08:14:25
问题 I have the following code: QPair<QSharedPointer<unsigned int>, int> someclass::somefunction() { int siz = data_size(); QSharedPointer<unsigned int> buffer(new unsigned int[siz]); // Fill the buffer... return qMakePair(buffer, siz); } At some point, the QSharedPointer returned by this function will go out of scope and the pointer set in the constructor will be free'd. Using valgrind 3.6.1, I get a "Mismatched free() / delete / delete[]" error. Is there anything wrong with my use of

Why does it look like boost::shared_ptr constructions are getting slower?

时光毁灭记忆、已成空白 提交于 2020-01-17 03:04:37
问题 I have a problem with boost shared_ptr. The initialization time of the smart pointer in the cycle is increased after the first iteration. The first iteration takes 40 msec. Every other iteration takes about 400 msec. I have no idea why it happens. I checked and there are no memory leaks and all destructors are called. Does anyone have a solution of this case? PS. However, when I use the boost::ptr_vector, the time is not increased( but only in debug version :) ). See example: class A; typedef

Why does it look like boost::shared_ptr constructions are getting slower?

穿精又带淫゛_ 提交于 2020-01-17 03:03:26
问题 I have a problem with boost shared_ptr. The initialization time of the smart pointer in the cycle is increased after the first iteration. The first iteration takes 40 msec. Every other iteration takes about 400 msec. I have no idea why it happens. I checked and there are no memory leaks and all destructors are called. Does anyone have a solution of this case? PS. However, when I use the boost::ptr_vector, the time is not increased( but only in debug version :) ). See example: class A; typedef

how to defer delete operation of shared_ptr?

孤者浪人 提交于 2020-01-14 14:09:54
问题 I have created a pointer of sample class in main. I am passing this pointer to a function function1() . This function has to use pointer as shared pointer and do some operations using this pointer. During exit of function1() destructor of sample in invoked due to shared_ptr . When I pass the same pointer to different function, this pointer is no more valid and program crashes. 1.How do I defer delete operation ( destruction invocation) in function1() ? 2.What is the alternative way, so that I

Why doesn't std::shared_ptr use reference linking?

丶灬走出姿态 提交于 2020-01-13 16:28:13
问题 std::shared_ptr needs to allocate a control block on the heap which holds the reference count. There was another approach I learnt from http://ootips.org/yonat/4dev/smart-pointers.html which keeps all the references in a doubly linked list. It doesn't need additional allocations nor a counter but the reference object itself is larger. Is there a benchmark or any clear reason showing one implementation is better than the others? 回答1: The standard does in theory allow a linked list to be used,

The correct way of returning std::unique_ptr to an object of polymorphic class

被刻印的时光 ゝ 提交于 2020-01-13 07:47:06
问题 Let's say I have the following hierarchy of classes: struct Base { }; struct Derived : public Base { void DoStuffSpecificToDerivedClass() { } }; And the following factory method: std::unique_ptr<Base> factoryMethod() { auto derived = std::make_unique<Derived>(); derived->DoStuffSpecificToDerivedClass(); return derived; // does not compile } The problem is, the return statement does not compile, because std::unique_ptr does not have a copy constructor with covariance support (which makes sense