shared-ptr

defining shared_ptr causes segfault (CMake)

China☆狼群 提交于 2019-12-12 05:28:21
问题 While setting up a new project (using CMake, compiler is gcc version 5.2.1, ubuntu (15.10)), I wanted to use a shared_ptr. This simple main.cpp works fine: #include <iostream> #include <memory> using namespace std; int main() { cout<<"Hi there!"<<endl; return 0; } But just defining a shared_ptr will cause the program to crash with segfault before even writing "Hi there!". #include <iostream> #include <memory> using namespace std; int main() { cout<<"Hi there!"<<endl; shared_ptr<double> test;

Visual Studio 2012 error C2039: 'serialize' : is not a member of 'std::shared_ptr<_Ty>'

那年仲夏 提交于 2019-12-12 04:48:48
问题 I am stuck with this problem while compiling my static class library. I know that Boost doesn't officially support VS2012 but since this is my current development environment, I could really use some advice. I have been searching around but nothing so far has helped me. Sample code: Foo.h: #include "FooImpl.h" #include <boost/serialization/serialization.hpp> #include <boost/serialization/shared_ptr.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp>

shared pointer assertion fail after iteration in a loop

这一生的挚爱 提交于 2019-12-12 02:57:41
问题 I am new to shared_ptr from boost and am considering to iterate over my set to get the best object. EDIT: added information about first_world std::set<World::CPtr> first_world = ... // long call, but it gets a set of constant shared pointers to the class World, where various methods exist typedef boost::shared_ptr<World const> CPtr; World::CPtr best = *(first_world.begin()); for (World::CPtr lo : first_world) { if (best->getValue() >= lo->getValue() ){ best = lo; } } Later I want to use that

How to connect two elements of a vector?

自作多情 提交于 2019-12-12 01:54:27
问题 I would like to know if there is a way in C++ to connect two elements of a vector for example std::vector such that if one is changed, the other changes automatically. If not is there any other way to do so? Thanks. 回答1: Let's say you have two vectors containing instances of a same kind of Object . Then using shared_ptr<Object> you can refer to the same object: vector<shared_ptr<Object>> v1; vector<shared_ptr<Object>> v2; for(int i=0;i<3;i++) { v1.push_back(shared_ptr<Object>(new Object()));

Accessing shared_ptr for this pointer

北战南征 提交于 2019-12-12 01:22:17
问题 Is there a way to get access to the shared_ptr for this: e.g. #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> #include <cassert> class Y: public boost::enable_shared_from_this<Y> { public: void foo(); void bar(); boost::shared_ptr<Y> f() { return shared_from_this(); } }; void Y::foo() { boost::shared_ptr<Y> p(this); boost::shared_ptr<Y> q = p->f(); q->bar(); p.reset(); q.reset(); } void Y::bar() { std::cout << __func__ << std::endl; } int main() { Y y; y.foo(); }

Declaring a boost asio socket, acceptor and endpoint in a class headerfile

 ̄綄美尐妖づ 提交于 2019-12-12 00:28:32
问题 I have a TCP/IP server made with boost asio that is wrapped in a class. Now i want declare the socket, eindpoint and acceptor in the class headerfile so that i can make memberfunctions that use the socket. The server runs now in the class constructor. I tried to use a initialiser list like below: CommunicationModuleTCPIP::CommunicationModuleTCPIP(bool Server, string IPAdress, int PortNumber) : Sock(new boost::asio::ip::tcp::socket(IOService)); { // Constructor code But this gives compiler

2D array in file/program scope

孤街浪徒 提交于 2019-12-11 23:04:47
问题 I need an array which I can access from different methods, I have to allocate this array in main() and then let other functions like foo() get access to this array. This question helped me with allocating the array: defining a 2D array with malloc and modifying it I'm defining the array like this: char(*array)[100] = malloc((sizeof *array) * 25200); And I'm doing this in main() I can store 25200 strings in this array an access them by array[1] Is it now possible to access this array from

Compiler error while using shared_ptr with a pointer to a pointer

情到浓时终转凉″ 提交于 2019-12-11 18:54:08
问题 I am new to using smart pointers in C++ and my current issue is that I am converting C code to C++ (C++11/14/17) and I am having some problems understanding using shared_ptr with a pointer to pointer. I have derived a toy example which I believe illustrates the problem Following is the header file #include <memory> using std::shared_ptr; struct DataNode { shared_ptr<DataNode> next; } ; struct ProxyNode { shared_ptr<DataNode> pointers[5]; } ; struct _test_ { ProxyNode** flane_pointers; }; And

use_count becomes -1 when using shared_ptr in C++

末鹿安然 提交于 2019-12-11 15:50:27
问题 GenericStack.h #ifndef _GENERIC_STACK_TROFIMOV_H_ #define _GENERIC_STACK_TROFIMOV_H_ #include <memory> class GenericStack { struct StackNode { std::shared_ptr<void> _data; StackNode* _next; StackNode(const std::shared_ptr<void>& p, StackNode* next) : _data(p), _next(next) { } }; StackNode* _top; GenericStack(const GenericStack&); GenericStack& operator=(const GenericStack&); protected: GenericStack(); ~GenericStack(); void push(const std::shared_ptr<void>&); void pop(); std::shared_ptr<void>&

Why doesn't shared_ptr have a virtual descructor? (and how can I get around this?)

∥☆過路亽.° 提交于 2019-12-11 14:43:51
问题 I wanted to make a special version of shared_ptr that would perform specific operations when it was created or destroyed, but my plans appear to be foiled by the realization that shared_ptr 's destructor is non virtual, meaning when I override it, my pointers never get cleaned up when the last instance of them are destroyed. The only alternative that comes to mind is to build in this behavior into every class that I want to use with my hypothetical custom shared_ptr , and that's not feasible