C++ 中placement new 操作符使用方法
C++ 中placement new 操作符使用方法
placement new操作符能夠在分配內(nèi)存時指定內(nèi)存位置。下面的程序使用了placement new操作符和常規(guī)new操作符給對象分配內(nèi)存。
// 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;
}
執(zhí)行結(jié)果:
[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操作時存在兩個問題。首先,在創(chuàng)建第二個對象時,placement new操作符使用一個新對象來覆蓋用于第一個對象的內(nèi)存單元。顯然,如果類動態(tài)地為其成員分配內(nèi)存,這將引發(fā)問題。
其次,將delete用于pc2和pc4時,將自動調(diào)用為pc2和pc4指向的對象調(diào)用析構(gòu)函數(shù);然而,將delete[]用于buffer時,不會為使用布局new操作符創(chuàng)建的對象調(diào)用析構(gòu)函數(shù)。
為確定兩個單元不重疊,可以這樣做:
pc1 = new (buffer) JustTesting;
pc3 = new (buffer + sizeof(JustTesting)) JustTesting("Better Idea", 6);
其中指針pc3相對于pc1的偏移量為JustTesting對象的大小
第二個教訓是,如果使用placement new操作符來為對象分配內(nèi)存,必須確保其析構(gòu)函數(shù)被調(diào)用,但如何確保呢?
例如,在堆中創(chuàng)建的對象,可以這樣做:
delete pc2;
然而,對于使用placement new操作符創(chuàng)建的對象,不能像下面一樣調(diào)用delete
delete pc1; // NO!!!
原因在于delete可與常規(guī)new操作符配合使用,但不能與placement new操作符配合使用。
那么我們要顯示調(diào)用析構(gòu)函數(shù),必須指定要銷毀的對象:
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語言_構(gòu)建一個靜態(tài)二叉樹實現(xiàn)方法
欄 目:C語言
下一篇:C語言使用openSSL庫DES模塊實現(xiàn)加密功能詳解
本文標題:C++ 中placement new 操作符使用方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1527.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載


