欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

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

placement new操作符能夠在分配內(nèi)存時(shí)指定內(nèi)存位置。下面的程序使用了placement new操作符和常規(guī)new操作符給對(duì)象分配內(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操作時(shí)存在兩個(gè)問(wèn)題。首先,在創(chuàng)建第二個(gè)對(duì)象時(shí),placement new操作符使用一個(gè)新對(duì)象來(lái)覆蓋用于第一個(gè)對(duì)象的內(nèi)存單元。顯然,如果類動(dòng)態(tài)地為其成員分配內(nèi)存,這將引發(fā)問(wèn)題。

     其次,將delete用于pc2和pc4時(shí),將自動(dòng)調(diào)用為pc2和pc4指向的對(duì)象調(diào)用析構(gòu)函數(shù);然而,將delete[]用于buffer時(shí),不會(huì)為使用布局new操作符創(chuàng)建的對(duì)象調(diào)用析構(gòu)函數(shù)。

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

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

 其中指針pc3相對(duì)于pc1的偏移量為JustTesting對(duì)象的大小

  第二個(gè)教訓(xùn)是,如果使用placement new操作符來(lái)為對(duì)象分配內(nèi)存,必須確保其析構(gòu)函數(shù)被調(diào)用,但如何確保呢?

  例如,在堆中創(chuàng)建的對(duì)象,可以這樣做:

delete pc2;

然而,對(duì)于使用placement new操作符創(chuàng)建的對(duì)象,不能像下面一樣調(diào)用delete

delete pc1; // NO!!!

  原因在于delete可與常規(guī)new操作符配合使用,但不能與placement new操作符配合使用。

那么我們要顯示調(diào)用析構(gòu)函數(shù),必須指定要銷毀的對(duì)象:

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

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

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有