欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年05月23日 17:03:06   投稿:lqh  
這篇文章主要介紹了C++ 中placement new 操作符使用方法的相關(guān)資料,需要的朋友可以參考下

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對象的大小

  第二個教訓(xùn)是,如果使用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;
}

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

相關(guān)文章

  • 給喜歡的人用C語言寫煙花

    給喜歡的人用C語言寫煙花

    你向窗外看煙火,我在窗邊看你,這時,你比煙花好看的多,你的眼眸倒映滿天的煙火,我的瞳孔倒影你閃光的眼色,這時,我比煙花寂寞
    2021-11-11
  • C語言實現(xiàn)靜態(tài)鏈表的方法

    C語言實現(xiàn)靜態(tài)鏈表的方法

    分享一段代碼,一個靜態(tài)鏈表的C語言實現(xiàn),其中包含著一種簡單的內(nèi)存管理策略:固定大小的鏈式管理。
    2013-03-03
  • 一篇文章帶你了解C++面向?qū)ο缶幊?-繼承

    一篇文章帶你了解C++面向?qū)ο缶幊?-繼承

    這篇文章主要介紹了解析C++面對象編程--繼承的運用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-08-08
  • C語言制作掃雷游戲(圖形庫)

    C語言制作掃雷游戲(圖形庫)

    這篇文章主要為大家詳細介紹了C語言制作掃雷游戲,結(jié)合圖形庫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語言基于貪心算法解決裝箱問題的方法

    C語言基于貪心算法解決裝箱問題的方法

    這篇文章主要介紹了C語言基于貪心算法解決裝箱問題的方法,簡單描述了裝箱問題,并結(jié)合實例形式給出了C語言使用貪心算法解決貪心問題的相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • C++17中的std::optional的具體使用

    C++17中的std::optional的具體使用

    這篇文章主要介紹了C++17中的std::optional的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C++設(shè)計模式之適配器模式(Adapter)

    C++設(shè)計模式之適配器模式(Adapter)

    這篇文章主要為大家詳細介紹了C++設(shè)計模式之適配器模式Adapter,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • return和break的區(qū)別解析

    return和break的區(qū)別解析

    這篇文章主要介紹了return和break的區(qū)別解析,需要的朋友可以參考下
    2014-02-02
  • C++ 中將一維數(shù)組轉(zhuǎn)成多維的三種方式示例詳解

    C++ 中將一維數(shù)組轉(zhuǎn)成多維的三種方式示例詳解

    這篇文章主要介紹了C++ 中將一維數(shù)組轉(zhuǎn)成多維的三種方式,每種方式結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • C++ list-map鏈表與映射表的簡單使用

    C++ list-map鏈表與映射表的簡單使用

    本文主要介紹了C++ list-map鏈表與映射表的簡單使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05

最新評論