国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-01-26 14:07:41
字體:
來源:轉載
供稿:網友

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", 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;}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霍林郭勒市| 朝阳区| 延安市| 衡阳县| 新宁县| 祁东县| 米林县| 浦县| 中江县| 渝中区| 岚皋县| 张家界市| 确山县| 渑池县| 达尔| 威远县| 简阳市| 静宁县| 扎鲁特旗| 镇赉县| 西乡县| 福州市| 石阡县| 南靖县| 贵定县| 万山特区| 徐汇区| 简阳市| 西峡县| 潞城市| 万全县| 博客| 永昌县| 铜山县| 阳山县| 武邑县| 日照市| 广饶县| 繁昌县| 南康市| 宁德市|