草庐IT

shared-primary-key

全部标签

c++ - 如果我将 std::shared_ptr 重置为自身会发生什么

以下程序因错误的glibcdoublefree错误而崩溃:#include#includeclassfoo{public:foo(){std::cout();std::cout由此我得到以下输出(后跟glibc错误):fooconstructedBeforeresetfoodestructedAfterresetfoodestructed很明显,在这种情况下,对象被销毁了两次。一次通过重置,一次通过std::shared_ptr超出范围。这实际上是我所期望的。关于cppreference但是我找到了以下文本(位于http://en.cppreference.com/w/cpp/memo

c++ - 在 hashmap/unordered_map 中,当 value 已经包含 key 时,是否可以避免数据重复

给定以下代码:structItem{std::stringname;intsomeInt;stringsomeString;Item(conststd::string&aName):name(aName){}};std::unordered_mapitems;Item*item=newItem("testitem");items.insert(make_pair(item.name,item);项目名称将在内存中存储两次-一次作为项目结构的一部分,一次作为map条目的键。是否可以避免重复?对于大约100M的记录,这种开销变得巨大。注意:我需要在Item结构中包含名称,因为我使用hash

c++ - 将 shared_ptr 传递给 OpenGL?

如果我的代码通常会像这样运行:char*log=newchar[logLength];glGetProgramInfoLog(...,...,log)//PrintLogdelete[]log;如何使用C++11智能指针实现相同的结果?谁知道在我有机会删除那段内存之前会发生什么。所以我想我需要向下转换为C风格的指针? 最佳答案 如果您的代码在您的代码片段中确实看起来像那样,shared_ptr对这种情况有点矫枉过正,因为看起来您不需要分配内存的共享所有权。unique_ptr对数组有部分专门化,非常适合此类用例。当托管指针超出范围时

2023版idea ssh 远程linux docker 报错: Only key-pair ssh auth type is supported for docker connections.

2023版ideassh远程linuxdocker报错:Cannotconnect:java.lang.llegalArgumentException:Onlykey-pairsshauthtypeissupportedfordockerconnections.环境:idea2023.3.2centos7安装docker报错截图:正确操作步骤:idea选择连接方式ssh点“+”号依次填入信息,点击“testConnection”,初次会报错,参考第4步报错,可以忽略,点击“OK”依次点击“Apply”,点击“OK”,关闭此界面下面的弹窗也“OK”关闭双击此处“Docker”,即可连接成功,再次

c++ - 将 shared_ptr 设置为指向现有对象

对于下面的代码,我想知道如何设置std::shared_ptr以指向两个成员函数中的给定对象。在main函数中分配的Vector3对象直到程序结束才会被删除。#include#includeusingstd::vector;usingstd::shared_ptr;classVector3{//...};classFace{vector>vtx;public:voidaddVtx(constVector3&vt){//vtx.push_back();0&&vvec(3);Faceface;face.addVtx(vec[0]);face.setVtx(0,vec[0])return0;

c++ - std::shared_ptr<T>:指向 T 的右值指针的隐式构造函数

我非常支持制作std::shared_ptr的想法接受T*的构造函数明确的。当您正在寻找堆损坏的原因时,它有助于避免不眠之夜。ScottMeyers对此给出了很好的解释。但是……如果我给它一个rvalue这不是明确的指针吗?我可以做这样的事情:///(1)std::shared_ptrt=newT;或///(2)T*giveaway=newT;std::shared_ptrt=std::move(giveaway);或者现实生活中更痛苦的案例///(3)voidfoo(std::shared_ptrt);///...foo(newT);对于我来说,所有这些案例都足够明确了。案例(1)是

c++ - Qt 信号 lambda 导致 shared_ptr 泄漏?

我有以下代码:#include#include#include#includeclassDocument{public:Document(){qDebug("Document");}~Document(){qDebug("~Document");}QUndoStackmUndostack;};classDocumentRepository{public:DocumentRepository(){qDebug("DocumentRepository");}~DocumentRepository(){qDebug("~DocumentRepository");}voidAddDoc(std

c++ - 使用 shared_ptr 的内部指针的操作是原子的吗?

同时复制和重置shared_ptr是否安全?即考虑下面的代码//Mainthread(beforecreatinganyotherthreads)shared_ptra(newA(1));//Thread1shared_ptra_copy=a;//Thread2a.reset(new(A(2));其中线程1和2并行运行。我可以确定a_copy将存储指向较旧的A(1)或较新的A(2)共享对象的指针吗? 最佳答案 来自cppreference:Allmemberfunctions(includingcopyconstructorandc

c++ - shared_ptr 中对 const int 的 undefined reference

我有一个配置类//config.hppclassConfig{public:staticconstexprinta=1;staticconstexprintb=1;}并包含在main.cpp中//main.cpp#include"config.hpp"intmain(){std::coutstream=std::make_shared(Config::a);//compileerror}编译器说未定义对Config::a的引用它在使用cout时有效,但在shared_ptr构造函数中时无效。我不知道为什么会这样。 最佳答案 请注意s

c++ - 将 googlemock EXPECT_CALL 与 shared_ptr 一起使用?

我有一个测试可以很好地使用原始指针,但我无法让它与std::shared_ptr一起工作。类是这样的:classMyClass{MyClass(SomeService*service);voidDoIt();}我的测试代码是这样的:classMyClassTests:public::testing::Test{public:MyClassTests():myClass_(newMyClass(&service_)){}protected:SomeServiceFakeservice_;MyClassSharedPointermyClass_;};TEST_F(MyClassTests,