草庐IT

gray8_ptr_t

全部标签

c++ - std::unique_ptr 和指向指针的指针

我想结合使用std::unique_ptr和FreeImage的FITAG。普通C中的代码将是:...loadimage;FITAG*tag=NULL;FreeImage_GetMetadata(FIMD_EXIF_EXIF,bitmap,"Property",&tag);...dosomestuffwithtag;FreeImage_DeleteTag(tag);...deleteimage;我对unique_ptr的尝试:std::unique_ptrtag(NULL,&FreeImage_DeleteTag);FreeImage_GetMetadata(FIMD_EXIF_EXI

c++ - 一个简单的 std::shared_ptr 构造案例的段错误

来自cppreference我了解到std::shared_ptr有一个构造函数:templateexplicitshared_ptr(Y*ptr);然后我试了一段代码如下:#include#include#includeintmain(void){///block1{std::shared_ptrs1(newstd::string("good"));std::shared_ptrs2(s1.get());///s2std::cerri1(newint(1));std::shared_ptri2(i1.get());///i2std::cerr它会导致block1的段错误,但不会导致b

c++ - 放置指向 shared_ptr 的多重映射的指针不起作用

vector工作正常Headerstd::vector>subnodes_m;DefinitionvoidCompositeSceneNode::AddChild(SceneNode*subnode_p){subnodes_m.emplace_back(subnode_p);}multimap没有Headerstd::multimap>subnodes_m;DefinitionvoidCompositeSceneNode::AddChild(SceneNode*subnode_p,unsignedintlayerIndex){subnodes_m.emplace(layerIndex,

c++ - 当shared_ptr,当unique_ptr

这个问题在这里已经有了答案:Differencesbetweenunique_ptrandshared_ptr[duplicate](4个答案)关闭7年前。什么时候应该使用shared_ptr什么时候使用unique_ptr?例如在这个类中而不是node*应该是shared_ptr或unique_ptr。它取决于什么?classnode{private:node*parent;vectorchildren;/**txny*x-numerdrzewa*y-numerwezla*/stringid;typeNodetype;//0-term,1-funcpublic:node(node*p

c++ weak_ptr在取消引用后过期?

我是智能指针的新手,我正在思考为什么weak_ptr在取消引用运算符后会过期。我用来测试的代码在这里:#include#include#includeusingnamespacestd;structnode{weak_ptrparent;shared_ptrchild;intval;};shared_ptrfoo(){shared_ptra=make_shared();shared_ptrb=make_shared();a->val=30;b->val=20;b->parent=a;a->child=b;returna;}intmain(){shared_ptrc=foo();node

c++ - 如何通过 unsigned long 将 std::shared_ptr 传递给回调?

我有一个旧的C风格库,它使用带有unsignedlong作为用户参数的回调,我想将我的shared_ptr传递给回调,以便增加引用计数。voidcallback(unsignedlongarg){std::shared_ptrptr=???arg???}voidstarter_function(){std::shared_ptrptr=std::make_shared();unsignedlongarg=???ptr???//passtolibrarysoitmaybeusedbycallback}目前我在shared_ptr上使用get(),然后使用C风格的强制转换,但这会在star

c++ - 将 std::unique_ptr 重置为指向数组的指针有什么问题?

我看到如下代码片段:std::unique_ptrmCache;mCache.reset(newuint8_t[size]);有人告诉我这段代码有一些问题。谁能给我一些细节? 最佳答案 给定std::unique_ptrmCache;,当mCache被摧毁了它的deleter将使用delete销毁被管理的指针(如果有的话),即为单个对象释放内存。但是在mCache.reset(newuint8_t[size]);之后什么mCachemanages是指向数组的指针,这意味着它应该使用delete[]反而;使用delete为数组释放内存

c++ - 从列表中删除 boost::shared_ptr 的正确方法是什么?

我有一个std::list的boost::shared_ptr我想从中删除一个项目,但我只有一个T*类型的指针,它与列表中的一个项目匹配。但是我不能使用myList.remove(tPtr)我猜是因为shared_ptr没有实现==为其模板参数类型。我的直接想法是尝试myList.remove(shared_ptr(tPtr))这在语法上是正确的,但它会因临时shared_ptr后的双重删除而崩溃有一个单独的use_count。std::list>myList;T*tThisPtr=newT();//Thisiswrong;onlydoneforexamplecode.//stand-

c++ - 如何从 vector 中删除 shared_ptr

像下面的代码,m_vSprites是shred_ptr的vector,如果他的一个元素更新失败,我想从vector中删除它,但是当我想使用删除时我的代码崩溃了。但我不明白为什么,有人可以帮忙吗?我需要使用erase的原因是因为我的应用程序会不断地向vector中添加元素,但如果某些元素满足它们的终止条件,也会不断地从vector中删除对象。如果我不删除它,vector会随着程序的运行而变得巨大!RECTrcOldSpritePos;typedefboost::shared_ptrSmartSprite;vector::iteratorsiSprite;for(siSprite=m_vS

c++ - std::tr1::shared_ptr 是否会抛出 bad_alloc 并且在 try/catch block 中是个好主意?

我实际上正在制作一个简单的C++SFML游戏,我想学习更多关于C++编程的知识。现在我正在使用shared_ptr来管理资源。创建新资源时,我对shared_ptrs有一些疑问,例如:shared_ptrresource(newResource(World::LEVEL));根据boostshared_ptr(Y*p)throwsbad_alloc。我不知道std::tr1是否也这样做。而且我不知道我是否应该担心将shared_ptr放入try/catchblock中以检查是否抛出bad_alloc。这是一个好的编程习惯吗? 最佳答案