草庐IT

Shared_ptr

全部标签

c++ - 使用 make_unique 语句重新分配 unique_ptr 对象 - 内存泄漏?

我不明白下面的语句会做什么(特别是第二行)?autobuff=std::make_unique(128);buff=std::make_unique(512);第二次调用make_unique后跟赋值运算符会释放第一次调用分配的内存,还是会发生内存泄漏?我必须使用buff.reset(newint[512]);吗?我调试了它,但没有发现任何operator=被调用,也没有发现任何析构函数被调用(通过unique_ptr)。 最佳答案 移动赋值运算符被调用,它执行if(this!=&_Right){//different,dothes

c++ - 错误 C2280 : attempting to reference a deleted function (unique_ptr)

我检查了一些使用原始指针的旧代码,并将它们更改为unique_ptr。现在,当我尝试编译代码时,收到此错误消息:Error1errorC2280:'std::unique_ptr>::unique_ptr(conststd::unique_ptr>&)':attemptingtoreferenceadeletedfunctiond:\visualstudio2013\vc\include\xmemory0关于这种情况的编译器输出很大——为了节省这个问题的空间,请参阅here.据我所知,这与我使用唯一指针的方式有关。它从这里开始(level.h,第65-66行):typedefstd::

c++ - 如何正确转发unique_ptr?

转发std::unique_ptr的正确方法通常是什么?以下代码使用了std::move,我认为这是经过深思熟虑的做法,但它因clang而崩溃。classC{intx;}unique_ptrtransform1(unique_ptrp){returntransform2(move(p),p->x);//transform2(unique_ptrp,intval){p->x*=val;returnp;}在通过std::move将所有权转移到下一个函数之前,是否有比简单地确保从p获得所需的一切更稳健的约定?在我看来,在对象上使用move并访问它以向同一函数调用提供参数可能是一个常见的错误。

c++ - 如何处理不断发展的 C++ std::namespace?例如:std::tr1::shared_ptr 对比 std::shared_ptr 对比 boost::shared_ptr 对比 boost::tr1::shared_ptr

对于我目前正在处理的代码,我们有时需要使用较旧的编译器在一些较旧的系统上进行编译(例如,我们在较旧的IBMBlueGene/L上运行sims,其支持契约(Contract)规定了一些非常旧的C++编译器)。代码本身使用了shared_ptr,最初是为使用std::tr1::shared_ptr而编写的。在旧的BlueGene机器上编译时,我很快意识到它没有tr1::实现,所以我切换到boost::shared_ptr。原来还有一个boost::tr1::shared_ptr。现在代码在我们的研究小组之外得到了更广泛的使用,可移植性变得更加重要。在大型代码库中处理这些不断演变的标准库问题

c++ - scoped_ptr 所有权

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:WhatisasmartpointerandwhenshouldIuseone?我正在阅读anarticle我找到了一个小例子来演示boost::scoped_ptr的使用:#include#include#include#includestaticintcount=0;classprinter{intm_id;public:printer(void):m_id(count++){}~printer(void){std::coutp1(newprinter);boost::scoped_ptrp2(newpri

c++ - C++1z 中 std::make_unique 和 std::make_shared 的有用性

在C++17中,我们可以对类模板进行模板类型推导。所以很多make函数可能会过时。make_unique和make_shared怎么样?所以我们可以这样写unique_ptrmyPtr(newMyType());//vsautomyPtr=make_unique();那么我们可以忘记那些功能吗? 最佳答案 unique_ptr和shared_ptr都不能在没有明确提供类型的情况下构造,因为无法区分T*和T[]。编写unique_ptr{newint}格式错误。此外,std::make_shared不仅仅为您构造一个std::shar

c++ - 为什么需要一个空的 shared_ptr 以及如何使用它?

在ScottMeyers的EffectiveC++中,第18项使接口(interface)易于正确使用且难以错误使用,他提到了nullshared_ptr:std::tr1::shared_ptrpInv(static_cast(0),getRidOfInvestment)和时尚分配操作pInv=...//makeretValpointtothecorrectobject在哪种情况下可能需要创建一个空的shared_ptr并稍后进行赋值?为什么不只要有资源(原始指针)就创建shared_ptr?由于ScottMeyers没有在前面的示例中显示完整的赋值,我认为shared_ptr的赋值

c++ - enable_shared_from_this(c++0x): what am I doing wrong?

我只是在研究即将推出的新C++标准中的智能指针。但是我没有掌握shared_from_this函数的用法。这是我所拥有的:#include#includeclassCVerboseBornAndDie2:publicstd::enable_shared_from_this{public:std::stringm_Name;CVerboseBornAndDie2(std::stringname):m_Name(name){std::coutp=vbad->shared_from_this();}并在行中抛出std::bad_weak_ptr异常std::shared_ptrp=vbad-

c++ - 关于 shared_ptr 析构函数中实现错误的困惑

我刚看到HerbSutter的演讲:C++andBeyond2012:HerbSutter-atomicWeapons,2of2他展示了std::shared_ptr析构函数实现中的错误:if(control_block_ptr->refs.fetch_sub(1,memory_order_relaxed)==0)deletecontrol_block_ptr;//B他说,由于memory_order_relaxed,delete可以放在fetch_sub之前。At1:25:18-Releasedoesn'tkeeplineBbelow,whereitshouldbe这怎么可能?存在h

c++ - std::list<std::unique_ptr>:空初始化列表与默认构造函数

代码#include#includeclassB;classA{std::list>bs;public:A();~A();};intmain(){Ax;return0;}显然编译。它没有链接,因为A::A()和A::~A()丢失了,但这是预料之中的。改变std::list>bs;应该调用std::list的标准构造函数list():list(Allocator()){}(C++14及以上)到std::list>bs{};应该调用list(std::initializer_list,constAllocator&=Allocator());默认构造函数也是。(感谢NicolBolas,他