什么相当于以下内容:
std::vector<Foo*> vec;
vec.push_back(NULL);
Run Code Online (Sandbox Code Playgroud)
什么时候处理boost::shared_ptr?它是以下代码吗?
std::vector< boost::shared_ptr<Foo> > vec;
vec.push_back(boost::shared_ptr<Foo>());
Run Code Online (Sandbox Code Playgroud)
注意:我可能会推回很多这样的对象.我应该在nullPtr某处声明一个全局静态对象吗?这样只需要构建其中一个:
boost::shared_ptr<Foo> nullPtr;
Run Code Online (Sandbox Code Playgroud) 我一直在使用pimpl成语制作一些对象,但我不确定是否使用std::shared_ptr或std::unique_ptr.
据我所知,std::unique_ptr是更有效的,但是这是没有这么多的问题,对我来说,因为这些物体是比较重量级反正做的成本std::shared_ptr比std::unique_ptr相对较小.
我目前std::shared_ptr只是因为额外的灵活性.例如,使用a std::shared_ptr允许我将这些对象存储在散列映射中以便快速访问,同时仍然能够将这些对象的副本返回给调用者(因为我相信任何迭代器或引用可能很快变得无效).
但是,这些对象确实没有被复制,因为更改会影响所有副本,所以我想知道也许使用std::shared_ptr和允许副本是某种反模式或坏事.
它是否正确?
使用make_shared<T>()而不是使用是否有任何缺点shared_ptr<T>(new T).
提升文档状态
用户一再请求工厂函数创建给定类型的对象并向其返回shared_ptr.除了方便和风格之外,这样的函数也是异常安全且相当快的,因为它可以对对象及其相应的控制块使用单个分配,从而消除了shared_ptr的构造开销的很大一部分.这消除了关于shared_ptr的主要效率投诉之一.
我与尝试shared_ptr,并make_shared从C++ 11和编程的小玩具的例子来看看调用时什么是实际发生的事情make_shared.作为基础设施,我使用llvm/clang 3.0以及XCode4中的llvm std c ++库.
class Object
{
public:
Object(const string& str)
{
cout << "Constructor " << str << endl;
}
Object()
{
cout << "Default constructor" << endl;
}
~Object()
{
cout << "Destructor" << endl;
}
Object(const Object& rhs)
{
cout << "Copy constructor..." << endl;
}
};
void make_shared_example()
{
cout << "Create smart_ptr using make_shared..." << endl;
auto ptr_res1 = make_shared<Object>("make_shared");
cout << "Create smart_ptr using make_shared: done." …Run Code Online (Sandbox Code Playgroud) 如果我理解正确,a weak_ptr不会增加托管对象的引用计数,因此它不代表所有权.它只是让您访问一个对象,其生命周期由其他人管理.所以我真的不明白为什么一个weak_ptr不能用a构建unique_ptr,而只能用a 构建shared_ptr.
有人能简单解释一下吗?
使用时,你有什么方法可以用脚射击自己boost::shared_ptr?换句话说,当我使用时,我必须避免哪些陷阱boost::shared_ptr?
当比较两个指针变体 - 经典与shared_ptr时 - 我对程序运行速度的显着提高感到惊讶.为了测试2D Delaunay增量插入算法已被使用.
编译器设置:
VS 2010(发布)/ O2/MD/GL,W7 Prof,CPU 3.GHZ DualCore
结果:
shared_ptr(C++ 0x00):
N[points] t[sec]
100 000 6
200 000 11
300 000 16
900 000 36
Run Code Online (Sandbox Code Playgroud)
指针:
N[points] t[sec]
100 000 0,5
200 000 1
300 000 2
900 000 4
Run Code Online (Sandbox Code Playgroud)
shared_ptr版本的运行时间大约是其10倍.这是由编译器设置引起的还是C++ 0x00 shared_ptr实现那么慢?
VS2010 Profiler:对于原始指针,大约60%的时间花费在启发式搜索包含插入点的三角形上(这是一个众所周知的事实).但是对于shared_ptr版本,大约58%的时间花在使用shared_ptr.reset()上,只有10%用于启发式搜索.
void DT2D::DT ( Node2DList *nl, HalfEdgesList *half_edges_dt, bool print )
{
// Create 2D Delaunay triangulation using incremental insertion method
unsigned int nodes_count_before = nl->size();
// Remove duplicit points …Run Code Online (Sandbox Code Playgroud) 更新
[X] 我发现
TLS::credentials creds在全局范围内声明时会发生这种情况,但是如果我在外部声明它不会发生段错误。我需要它是全局的,因为它有助于缓存证书,并且多个线程可以使用其他线程创建的证书,而无需花时间创建新证书。
[X] 我进一步减少了大约 200 行的代码。到 100 行
我正在使用 Botan 创建一个 TLS 应用程序,我的应用程序在应用程序结束时因段错误而崩溃。
我试图用 Valgrind 调试它,但它无济于事。
这是来自 Valgrind 的堆栈跟踪,
==3841967== Invalid write of size 8
==3841967== at 0x4842964: memset (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==3841967== by 0x566A82F: Botan::deallocate_memory(void*, unsigned long, unsigned long) (in /usr/lib/x86_64-linux-gnu/libbotan-2.so.12.12.1)
==3841967== by 0x55E1A4D: ??? (in /usr/lib/x86_64-linux-gnu/libbotan-2.so.12.12.1)
==3841967== by 0x40EC7B: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:155)
==3841967== by 0x40EC29: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:730)
==3841967== by 0x41112D: std::__shared_ptr<Botan::RSA_Public_Data const, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:1169)
==3841967== by 0x411107: std::shared_ptr<Botan::RSA_Public_Data const>::~shared_ptr() (shared_ptr.h:103)
==3841967== by 0x41109D: Botan::RSA_PublicKey::~RSA_PublicKey() (rsa.h:25) …Run Code Online (Sandbox Code Playgroud) 我有类似于shared_ptr<Type> t(makeSomething(), mem_fun(&Type::deleteMe))
我现在需要调用需要指针的C样式函数Type.我怎样才能得到它shared_ptr?
我在typedef的boost :: shared_ptr模板的命名约定之间翻转.例如:
typedef boost::shared_ptr<Foo> FooPtr;
Run Code Online (Sandbox Code Playgroud)
在制定一项公约之前,我想看看其他人使用什么.你的约定是什么?
编辑:
对于那些在Foo中嵌入typedef的人来说,Foo现在"意识到"它将如何被传递并不会让你烦恼吗?它似乎打破了封装.这个怎么样:
class Foo
{
public:
typedef std::vector<Foo> Vector
};
Run Code Online (Sandbox Code Playgroud)
你现在不会这样做,对吗?:-)
c++ ×10
shared-ptr ×10
boost ×5
c++11 ×2
pointers ×2
unique-ptr ×2
botan ×1
clang ×1
libc++ ×1
make-shared ×1
null ×1
performance ×1
pimpl-idiom ×1
weak-ptr ×1