草庐IT

allocate_shared

全部标签

c++ - shared_ptr 的循环依赖问题是什么?

我阅读了共享指针并了解如何使用。但我一直不明白共享指针的循环依赖问题以及弱指针将如何解决这些问题。谁能解释清楚这个问题? 最佳答案 问题没那么复杂。让-->代表一个共享指针:Therestoftheprogram-->objectA-->objectB^|\|\vobjectC所以我们得到了一个共享指针的循环依赖。每个对象的引用计数是多少?A:2B:1C:1现在假设程序的其余部分(或者至少是其中包含指向A的共享指针的部分)被销毁了。那么A的引用计数减1,所以循环中每个对象的引用计数都为1。那么删除了什么?没有什么。但是我们要删除什么

c++ - new 和 make_shared 用于共享指针

我遇到了this@kerekSB状态的帖子和答案之一std::shared_ptrp1=std::make_shared("foo");std::shared_ptrp2(newObject("foo"));Inyourcode,thesecondvariableisjustanakedpointer,notasharedpointeratall.Nowonthemeat.make_sharedis(inpractice)moreefficient,becauseitallocatesthereferencecontrolblocktogetherwiththeactualobject

c++ - 如何在中等规模的项目中诊断 g++ 错误 "cc1plus.exe: out of memory allocating 838860800 bytes"?

这个问题在这里已经有了答案:Running'gcc'onC++sourcefileonLinuxgives"cc1plus:outofmemoryallocating..."errormessage(2个答案)关闭6年前。我正在尝试移植我的C++library使用基本的g++makefile(它在VisualStudio中编译得很好)。我现在尝试编译的部分大约有45000行代码。库本身编译正常,但是当我尝试将它包含到控制台界面应用程序中时,编译器崩溃并显示以下消息,没有其他消息:cc1plus.exe:outofmemoryallocating838860800bytes当我包含项目的

c++ - 在编译时验证对象是否创建为 shared_ptr

我写的一些类(通常作为boost::asio的一部分)的对象依赖于包装在shared_ptr中,因为它们使用shared_from_this()。如果一个对象没有在shared_ptr中实例化,有没有办法阻止它被编译?所以,我要找的是:std::shared_ptra=std::make_shared();//shouldcompilefinestd::unique_ptra=std::make_unique();//compileerrorMyClassa;//compileerror 最佳答案 将其构造函数设为私有(privat

c++ - 转换 shared_ptr 类型 vector 的迭代器

如何转换shared_ptr类型vector的迭代器?考虑以下示例:typedefboost::shared_ptrtype_myClass;vectorvect;vector::iteratoritr=vect.begin();while(itr!=vect.end()){//Followingstatementworks,butIwishtorathercastthis//toMyClassandthencallafunction?(*itr)->doSomething();} 最佳答案 您不想转换,而是提取对该对象的引用:My

c++ - std::atomic 加载方法在与 std::shared_ptr 一起使用时减少引用计数

我想使用std::atomic在我的代码中,以便可以自动更新shared_ptr,但是在访问shared_ptr时我遇到了问题。atomic上的load()方法似乎减少了shared_ptr上的引用计数,因此我无法在不释放对象的情况下实际使用该对象。这是显示问题的一段简化代码...typedefshared_ptrMyClassPtr;typedefatomicMyClassAtomicPtr;//1.MyClassPtrptr(newMyClass());printf("1.use_count=%d\n",ptr.use_count());//2.MyClassAtomicPtrat

c++ - 了解 C++ std::shared_ptr

我有一个问题,请看下面这个简单的C++程序,intmain(){shared_ptrsptr1(newint);shared_ptrsptr2=sptr1;shared_ptrsptr3;shared_ptrsptr4;sptr3=sptr2;cout输出:333444sptr1和sptr3对象如何知道引用计数在打印4时递增。据我所知,引用计数是每个shared_ptr对象中的一个变量。 最佳答案 Asfarasiknowreferencecountisavariableineachshared_ptrobject.不,引用计数存储

c++ - 为什么不是 std::string::max_size() == std::string::allocator::max_size()

最近我注意到给定std::strings的情况下以下陈述不正确.s.max_size()==s.get_allocator().max_size();我发现这很有趣,默认情况下std::string将使用std::allocator其理论极限为size_type(-1)(是的,我知道我假设2的补码,但这与实际问题无关)。我知道实际限制会比这少得多。在典型的32位x86系统上,内核将占用2GB(可能是1GB)的地址空间,实际上限要小得多。无论如何,GNUlibstdc++的std::basic_string::max_size()似乎返回相同的值,不管它使用的分配器说什么(类似于1073

C++0x 错误:将带有 std::shared_ptr 的函数重载为 const 参数不明确

假设我有两个不相关类A和B。我还有一个类Bla使用boost::shared_ptr像这样:classBla{public:voidfoo(boost::shared_ptr);voidfoo(boost::shared_ptr);}注意const。这是这个问题的原始版本缺少的重要部分。这编译,下面的代码工作:Blabla;boost::shared_ptra;bla.foo(a);但是,如果我在上述示例中从使用boost::shared_ptr切换到使用std::shared_ptr,我会收到如下编译错误:"error:callofoverloaded'foo(std::shared

c++ - boost::make_shared 不是在调用(放置)运算符 new 吗?

我第一次使用boost::make_shared来创建共享指针指向的对象。主要是因为我们的代码太慢了,单次分配确实有助于boost性能。在以“硬手动方式”修复了一些内存泄漏之后,我决定通过覆盖所有相关类的新运算符来实现一个简单的内存泄漏检测器,仅用于计算在我们的应用程序的特定点哪些对象仍然存在。我之前已经实现过几次,惊讶地发现我的代码不再检测到任何对象。我认为我所要做的就是覆盖“placementnew”而不是“normal”operatornew,因为make_shared的boost网站文档中有以下内容:"Effects:Allocatesmemorysuitableforanob