C++ 中placement new *** 作符使用方法

C++ 中placement new  *** 作符使用方法,第1张

概述C++中placementnew *** 作使用方法placementnew *** 作符能够在分配内存时指定内存位置。下面的程序使用了placementnew *** 作符和常规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 constructedheap1 constructedMemory block address:buffer: 0x936a008  heap: 0x936a248Memory contents: 0x936a008: Just Testing,00x936a248: heap1,20bad IDea constructedHeap2 constructedMemory contents: 0x936a008: bad IDea,60x936a290: Heap2,10heap1 destroyedHeap2 destroyedDone

上面的程序使用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",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 *** 作符使用方法全部内容,希望文章能够帮你解决C++ 中placement new *** 作符使用方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1245049.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存