什么是正确的方法来创build一个unique_ptr持有在免费商店分配的数组? Visual studio 2013支持默认,但是当我在Ubuntu上使用gcc版本4.8.1时,我得到了内存泄漏和未定义的行为。
这个问题可以用这个代码重现:
#include <memory> #include <string.h> using namespace std; int main() { unique_ptr<unsigned char> testData(new unsigned char[16000]()); memset(testData.get(),0x12,0); return 0; }
Valgrind会给出这个输出:
==3894== 1 errors in context 1 of 1: ==3894== Mismatched free() / delete / delete [] ==3894== at 0x4C2BADC: operator delete(voID*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==3894== by 0x400AEF: std::default_delete<unsigned char>::operator()(unsigned char*) const (unique_ptr.h:67) ==3894== by 0x4009D0: std::unique_ptr<unsigned char,std::default_delete<unsigned char> >::~unique_ptr() (unique_ptr.h:184) ==3894== by 0x4007A9: main (test.cpp:19) ==3894== Address 0x5a1a040 is 0 bytes insIDe a block of size 16,000 alloc'd ==3894== at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==3894== by 0x40075F: main (test.cpp:15)
auto_ptr.release()中的分段错误
我如何传递boost :: shared_ptr作为指向windows线程函数的指针?
如何使用postThreadMessage来传递一个结构体
如何查看gdb中的智能指针的内部数据?
std :: tr1 :: shared_ptr是否互斥?
eclipse中的unique_ptr自动完成
unique_ptr编译错误
使用T[]专精:
std::unique_ptr<unsigned char[]> testData(new unsigned char[16000]());
请注意,在理想的情况下,您不必明确使用new来实例化unique_ptr ,避免了潜在的异常安全隐患。 为此,C ++ 14为您提供了std::make_unique函数模板。 看到这个优秀的GOTW的更多细节。 语法是:
auto testData = std::make_unique<unsigned char[]>(16000);
使用阵列版本:
auto testData = std::unique_ptr<unsigned char[]>{ new unsigned char[16000] };
或者用c ++ 14,更好的形式(VS2013已经有了):
auto testData = std::make_unique<unsigned char[]>( 16000 );
最有可能的更好的方法是使用std::vector<unsigned char>来代替
#include <vector> #include <string> using namespace std; int main() { vector<unsigned char> testData(0x12,0); // replaces your memset // bla }
这样做的好处在于,这种方法不容易出错,并且可以访问各种功能,例如简单的迭代,插入,到达容量时自动重新分配。
有一点需要注意:如果你将数据转移到很多地方, std::vector花费更多,因为它跟踪了大小和容量,而不仅仅是数据的开始。
注意:你的memset不做任何事情,因为你用一个零计数参数来调用它。
看起来像一个傻瓜,我会解释我的意思
class Object { private : static int count; public : Object() { cout << "Object Initialized " << endl; count++; } ~Object() { cout << "Object destroyed " << endl; } int print() { cout << "Printing" << endl; return count; } }; int Object::count = 0; int main(int argc,char** argv) { // This will create a pointer of Object unique_ptr<Object> up2 = make_unique<Object>(); up2->print(); // This will create a pointer to array of Objects,The below two are same. unique_ptr<Object[]> up1 = std::make_unique<Object[]>(30); Object obj[30]; cout << up1.get()[8].print(); cout << obj[8].print(); // this will create a array of pointers to obj. unique_ptr<Object*[]> up= std::make_unique<Object*[]>(30); up.get()[5] = new Object(); unique_ptr<Object> mk = make_unique<Object>(*up.get()[5]); cout << up.get()[5]->print(); unique_ptr<unique_ptr<Object>[]> up3 = std::make_unique<unique_ptr<Object>[]>(20); up3.get()[5] = make_unique<Object>(); return 0; }
这篇文章的目的是隐藏你需要了解的细微的东西。 创建对象数组与unique_ptr的对象数组相同。 只有当你在论证中通过它才会有所作为。 创建unique_ptr的对象指针数组也不是很有用。 所以只有在两个以下你需要在大多数情况下使用。
unique_ptr<Object> obj; //and unique_ptr<unique_ptr<Object>[]>= make_unique<unique_ptr<Object>[]>(20);
unsigned int size=16000; std::unique_ptr<unsigned char[],std::default_delete<unsigned char[]>> pData(new unsigned char[size]);
总结以上是内存溢出为你收集整理的正确的方法来创build持有分配数组的unique_ptr全部内容,希望文章能够帮你解决正确的方法来创build持有分配数组的unique_ptr所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)