C++ 中placement new 操作符使用方法
2019/7/10 22:47:04
本文主要是介绍C++ 中placement new 操作符使用方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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++ 中placement new 操作符使用方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升