C++ 中placement new 操作符使用方法
2017-05-23 17:03
323 查看
C++ 中placement new 操作符使用方法
placement new操作符能够在分配内存时指定内存位置。下面的程序使用了placement new操作符和常规new操作符给对象分配内存。
// placenew.cpp -- new, placement new, no delete #include <iostream> #include <string> #include <new> using namespace std; const int BUF = 512; class JustTesting { private: string words; int number; public: JustTesting(const string &s = "Just Testing", int n = 0) { words = s; number = n; cout << words << " constructed\n"; } ~JustTesting() { cout << words << " destroyed\n"; } void Show() const { cout << words << ", " << number << endl; } }; int main(void) { char *buffer = new char [BUF]; // get a block of memory JustTesting *pc1, *pc2; pc1 = new (buffer)JustTesting; // place object in buffer pc2 = new JustTesting("heap1", 20); // place object on heap cout << "Memory block address:\n" << "buffer: " << (void *)buffer << " heap: " << pc2 << endl; cout << "Memory contents: \n"; cout << pc1 << ": "; pc1->Show(); cout << pc2 << ": "; pc2->Show(); JustTesting *pc3, *pc4; pc3 = new (buffer) JustTesting("bad Idea", 6); pc4 = new JustTesting("Heap2", 10); cout << "Memory contents: \n"; cout << pc3 << ": "; pc3->Show(); cout << pc4 << ": "; pc4->Show(); delete pc2; // free heap1 delete pc4; // free heap2 delete [] buffer; // free buffer cout << "Done\n"; return 0; }
执行结果:
[root@localhost 桌面]# ./new Just Testing constructed heap1 constructed Memory block address: buffer: 0x936a008 heap: 0x936a248 Memory contents: 0x936a008: Just Testing, 0 0x936a248: heap1, 20 bad Idea constructed Heap2 constructed Memory contents: 0x936a008: bad Idea, 6 0x936a290: Heap2, 10 heap1 destroyed Heap2 destroyed Done
上面的程序使用placement new操作时存在两个问题。首先,在创建第二个对象时,placement new操作符使用一个新对象来覆盖用于第一个对象的内存单元。显然,如果类动态地为其成员分配内存,这将引发问题。
其次,将delete用于pc2和pc4时,将自动调用为pc2和pc4指向的对象调用析构函数;然而,将delete[]用于buffer时,不会为使用布局new操作符创建的对象调用析构函数。
为确定两个单元不重叠,可以这样做:
pc1 = new (buffer) JustTesting; pc3 = new (buffer + sizeof(JustTesting)) JustTesting("Better Idea", 6);
其中指针pc3相对于pc1的偏移量为JustTesting对象的大小
第二个教训是,如果使用placement new操作符来为对象分配内存,必须确保其析构函数被调用,但如何确保呢?
例如,在堆中创建的对象,可以这样做:
delete pc2;
然而,对于使用placement new操作符创建的对象,不能像下面一样调用delete
delete pc1; // NO!!!
原因在于delete可与常规new操作符配合使用,但不能与placement new操作符配合使用。
那么我们要显示调用析构函数,必须指定要销毁的对象:
pc3->~JustTesting(); // destroy object pointed to by pc3
int main(void) { char *buffer = new char[BUF]; // get a block of memory JustTesting *pc1, *pc2; pc1 = new (buffer) JustTesting; // place object in buffer pc2 = new JustTesting("Heap1", 20); // place object on heap cout << "Memory block addresses: /n" << "buffer: " << (void *)buffer << " heap: " << pc2 << endl; cout << "Memory contents: "; cout << pc1 << ": "; pc1->Show(); cout << pc2 << ": "; pc2->Show(); JustTesting *pc3, *pc4; // fix placement new location pc3 = new (buffer + sizeof(JustTesting)) JustTesting("better Idea", 6); pc4 = new JustTesting("Heap2", 10); cout << "Memory contents: "; cout << pc3 << ": "; pc3->Show(); cout << pc4 << ": "; pc4->Show(); delete pc2; // free heap1 delete pc4; // free heap2 // explicitly destroy placement new object pc3->~JustTesting(); // destroy object pointed to by pc3 pc1->~JustTesting(); // destroy object pointed to by pc1 delete []buffer; // free buffer cout << "Done/n"; return 0; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章推荐
- C++中static_cast, dynamic_cast使用方法
- C++中stringstream的使用方法和样例
- 使用doxygen从c++代码中生成文档的方法学习
- 使用 + 操作符、parseInt 、 parseFloat等方法处理数字字符串时的不同
- C++中四种类型转换运算符的使用方法(reinterpret_cast,const_cast,static_cast,dynamic_cast)
- C++中Static作用和使用方法
- c++模版的一些使用方法(三)
- c++模版的一些使用方法(四)
- C++控件:ListControl列表控件的使用方法
- C++中abs、fabs、fabsf的使用方法
- C++ STL中vector的使用方法
- windows mobile C++ 的SQLite使用方法
- C++ 排序函数 sort(),qsort()的使用方法
- 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
- C++ 标准库 vector list map使用方法
- C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)
- c++ 模版的使用方法
- C++中使用Socket通信包定义和读取方法
- C++ 中vector的使用方法
- Windows,C/C++_字符串转换_MultiByteToWideChar和WideCharToMultiByte的正确使用方法及参数详解