布局NEW操作符

來源:互聯網
上載者:User

而已NEW操作符能夠在分配記憶體時指定記憶體位置。下面的程式使用了布局new操作符和常規new操作符給對象分配記憶體。

// placenew.cpp -- new, placement new, no delete<br />#include <iostream><br />#include <string><br />#include <new></p><p>using namespace std;<br />const int BUF = 512;</p><p>class JustTesting<br />{<br />private:<br />string words;<br />int number;<br />public:<br />JustTesting(const string &s = "Just Testing", int n = 0)<br />{<br />words = s; number = n; cout << words << " constructed/n";<br />}<br />~JustTesting() { cout << words << " destroyed/n"; }<br />void Show() const { cout << words << ", " << number << endl; }<br />};</p><p>int main(void)<br />{<br />char *buffer = new char [BUF];// get a block of memory<br />JustTesting *pc1, *pc2;</p><p>pc1 = new (buffer)JustTesting;// place object in buffer<br />pc2 = new JustTesting("heap1", 20);// place object on heap</p><p>cout << "Memory block address:/n" << "buffer: "<br /><< (void *)buffer << "heap: " << pc2 << endl;<br />cout << "Memory contents: /n";<br />cout << pc1 << ": ";<br />pc1->Show();<br />cout << pc2 << ": ";<br />pc2->Show();</p><p>JustTesting *pc3, *pc4;<br />pc3 = new (buffer) JustTesting("bad Idea", 6);<br />pc4 = new JustTesting("Heap2", 10);</p><p>cout << "Memory contents: /n";<br />cout << pc3 << ": ";<br />pc3->Show();<br />cout << pc4 << ": ";<br />pc4->Show();</p><p>delete pc2;// free heap1<br />delete pc4;// free heap2<br />delete [] buffer;// free buffer<br />cout << "Done/n";</p><p>return 0;<br />}

   該程式使用new操作符建立了一個512位元組的記憶體緩衝區,然後使用new操作符在堆中建立兩個JustTesting對象,並試圖使用布局new操作符在記憶體緩衝區中建立兩個JustTesting對象。

   下面是我的電腦的輸出

<略>

 

    上面的程式使用布局new操作時存在兩個問題。首先,在建立第二個對象時,布局new操作符使用一個新對象來覆蓋用於第一個對象的記憶體單元。顯然,如果類動態地為其成員分配記憶體,這將引發問題。

     其次,將delete用於pc2和pc4時,將自動調用為pc2和pc4指向的對象調用解構函式;然而,將delete[]用於buffer時,不會為使用布局new操作符建立的對象調用解構函式。

   為確定兩個單元不重疊,可以這樣做:

pc1 = new (buffer) JustTesting;<br />pc3 = new (buffer + sizeof(JustTesting)) JustTesting("Better Idea", 6);

  

  其中指標pc3相對於pc1的位移量為JustTesting對象的大小

 

  第二個教訓是,如果使用布局new操作符來為對象分配記憶體,必須確保其解構函式被調用,但如何確保呢?

  例如,在堆中建立的對象,可以這樣做:

delete pc2;

然而,對於使用布局new操作符建立的對象,不能像下面一樣調用delete

delete pc1; // NO!!!

  原因在於delete可與常規new操作符配合使用,但不能與布局new操作符配合使用。

那麼我們要顯示調用解構函式,必須指定要銷毀的對象:

pc3->~JustTesting();   // destroy object pointed to by pc3

 

int main(void)<br />{<br />char *buffer = new char[BUF];// get a block of memory<br />JustTesting *pc1, *pc2;</p><p>pc1 = new (buffer) JustTesting;// place object in buffer<br />pc2 = new JustTesting("Heap1", 20);// place object on heap</p><p>cout << "Memory block addresses: /n" << "buffer: "<br /><< (void *)buffer << "heap: " << pc2 << endl;<br />cout << "Memory contents: ";<br />cout << pc1 << ": ";<br />pc1->Show();<br />cout << pc2 << ": ";<br />pc2->Show();</p><p>JustTesting *pc3, *pc4;<br />// fix placement new location<br />pc3 = new (buffer + sizeof(JustTesting)) JustTesting("better Idea", 6);<br />pc4 = new JustTesting("Heap2", 10);</p><p>cout << "Memory contents: ";<br />cout << pc3 << ": ";<br />pc3->Show();<br />cout << pc4 << ": ";<br />pc4->Show();</p><p>delete pc2;// free heap1<br />delete pc4;// free heap2<br />// explicitly destroy placement new object<br />pc3->~JustTesting();// destroy object pointed to by pc3<br />pc1->~JustTesting();// destroy object pointed to by pc1<br />delete []buffer;// free buffer<br />cout << "Done/n";</p><p>return 0;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.