shared-ptr

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

Correct way to allocate memory to std::shared_ptr

巧了我就是萌 提交于 2020-01-14 13:10:40
问题 I have implemented a function where the identity is given to me and out of my control. It returns std::shared_ptr<const void> . In the function i allocate an arbitrary amount of memory, and return access to it though the shared_ptr. My memory allocation is done with new unsigned char[123] . The problem is that valgrind detects a mismatch between the usage of new and delete variants. While i use new[](unsigned) to allocate memory, the shared_ptr destructor uses delete(void*) to deallocate it,

Create shared_ptr from reference

删除回忆录丶 提交于 2020-01-14 08:05:15
问题 I'm relativly new to C++ and this seams like a noob question but I wasn't able to solve it with other resources on the internet. I'm trying to create a shared_ptr from a reference. I have following Book class: #include <memory> #include "Author.hpp" class Book { public: void setAuthor(const Author& t_author); private: std::shared_ptr<Author> m_author; } And this is my Author class: #include <memory> class Book; class Author { public: void addBook(const Book& t_book); private: std::vector<std:

shared_ptr<T> to shared_ptr<T const> and vector<T> to vector<T const>

♀尐吖头ヾ 提交于 2020-01-14 07:28:05
问题 I'm trying to define a good design for my software which implies being careful about read/write access to some variables. Here I simplified the program for the discussion. Hopefully this will be also helpful to others. :-) Let's say we have a class X as follow: class X { int x; public: X(int y) : x(y) { } void print() const { std::cout << "X::" << x << std::endl; } void foo() { ++x; } }; Let's also say that in the future this class will be subclassed with X1, X2, ... which can reimplement

Smart pointer, why need to check if I am the only user before changing the underlying object?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-14 03:37:09
问题 I am reading C++ Primer and find these kinda confusing: The reset member is often used together with unique to control changes to the object shared among several shared_ptr s. Before changing the underlying object, we check whether we’re the only user. If not, we make a new copy before making the change: if (!p.unique()) p.reset(new string(*p)); // we aren't alone; allocate a new copy *p += newVal; // now that we know we're the only pointer, okay to change this object What does the emphasized

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,

Why must shared_ptr<> allocate for the control block and managed object separately?

こ雲淡風輕ζ 提交于 2020-01-13 12:16:24
问题 This linked question asked if the make_shared<> function and the shared_ptr<> constructor differ. What happens when using make_shared Part of the answer was that make_shared<> will usually allocate memory for both the pointed to object and smart pointer control block in a single allocation. The shared_ptr<> constructors use two allocations. cppreference states that the constructors "must" do so but no reason is given. Why is this? Is it for some reason impossible? Or is it forbidden by the

Strange shared_ptr behaviour

微笑、不失礼 提交于 2020-01-13 11:39:18
问题 Ive discovered a really strange behaviour with std::shared_ptr in c++. The following example works perfectly with standard pointers. However the usage of std::shared_ptr causes here a segmentation fault. (see the backtrace below) I know, that accessing a std::shared_ptr from multiple threads is not safe, therefor I'm using the atomic-operations. Even a classic lock wont solve the problem. Im using gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2) with -Wall -O2 -g and -std=c++17 Does

How to return references to object created inside a method

我的梦境 提交于 2020-01-13 10:35:07
问题 I am reasoning about the best approach to return references to objects created inside a method, like in the following situation: class A{ public: A(){} ~A(){} }; class Foo{ public: Foo(){} ~Foo(){} A& create(int random_arg){ // create object A and return its reference } }; void other_method(){ Foo f; A a = f.create(); // do stuff with a { I have considered three possible solutions: create a raw pointer and return a reference, but this is bad because there is no guarantee that the object will