日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++中的new和delete操作符重载

發布時間:2025/4/5 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中的new和delete操作符重载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 new和delete操作符重載
      • 1.1 new和delete操作符重載簡介
      • 1.2 靜態存儲區中創建動態對象
      • 1.3 在指定的地址上創建C++對象
    • 2 new[]和delete[]操作符重載

1 new和delete操作符重載

1.1 new和delete操作符重載簡介

new/delete的本質是C++預定義的操作符,C++對這兩個操作符做了嚴格的行為定義:

  • new:
  • 獲取足夠大的內存空間(默認為堆空間)。
  • 在獲取的空間中調用構造函數創建對象。
  • delete:
  • 調用析構函數銷毀對象。
  • 歸還對象所占用的空間(默認為堆空間)。

在C++中能夠重載new/delete操作符:

  • 全局重載(不推薦)
  • 局部重載(針對具體類進行重載)

重載new/delete的意義在于改變動態對象創建時的內存分配方式。

new和delete的重載方式:

需要注意的點:

  • new操作符重載函數返回后編譯器會自動調用構造函數。
  • delete操作符重載函數調用前編譯器會自動調用析構函數。

1.2 靜態存儲區中創建動態對象

#include <iostream> #include <string>using namespace std;class Test {static const unsigned int COUNT = 4;static char c_buffer[];static char c_map[];int m_value; public:void* operator new (unsigned int size){void* ret = NULL;for(int i=0; i<COUNT; i++){if( !c_map[i] ){c_map[i] = 1;ret = c_buffer + i * sizeof(Test);cout << "succeed to allocate memory: " << ret << endl;break;}}return ret;}void operator delete (void* p){if( p != NULL ){char* mem = reinterpret_cast<char*>(p);int index = (mem - c_buffer) / sizeof(Test);int flag = (mem - c_buffer) % sizeof(Test);if( (flag == 0) && (0 <= index) && (index < COUNT) ){c_map[index] = 0;cout << "succeed to free memory: " << p << endl;}}} };char Test::c_buffer[sizeof(Test) * Test::COUNT] = {0}; char Test::c_map[Test::COUNT] = {0};int main(int argc, char *argv[]) {cout << "===== Test Single Object =====" << endl;Test* pt = new Test;delete pt;cout << "===== Test Object Array =====" << endl;Test* pa[5] = {0};for(int i=0; i<5; i++){pa[i] = new Test;cout << "pa[" << i << "] = " << pa[i] << endl;}for(int i=0; i<5; i++){cout << "delete " << pa[i] << endl;delete pa[i];}return 0; }

1.3 在指定的地址上創建C++對象

解決方案:

  • 在類中重載new/delete操作符。
  • 在new的操作符重載函數中返回指定的地址。
  • 在delete操作符重載中標記對應的地址可用。

編程實驗:自定義動態對象的存儲空間

#include <iostream> #include <string> #include <cstdlib>using namespace std;class Test {static unsigned int c_count;static char* c_buffer;static char* c_map;int m_value; public:static bool SetMemorySource(char* memory, unsigned int size){bool ret = false;c_count = size / sizeof(Test);ret = (c_count && (c_map = reinterpret_cast<char*>(calloc(c_count, sizeof(char)))));if( ret ){c_buffer = memory;}else{free(c_map);c_map = NULL;c_buffer = NULL;c_count = 0;}return ret;}void* operator new (unsigned int size){void* ret = NULL;if( c_count > 0 ){for(int i=0; i<c_count; i++){if( !c_map[i] ){c_map[i] = 1;ret = c_buffer + i * sizeof(Test);cout << "succeed to allocate memory: " << ret << endl;break;}}}else{ret = malloc(size);}return ret;}void operator delete (void* p){if( p != NULL ){if( c_count > 0 ){char* mem = reinterpret_cast<char*>(p);int index = (mem - c_buffer) / sizeof(Test);int flag = (mem - c_buffer) % sizeof(Test);if( (flag == 0) && (0 <= index) && (index < c_count) ){c_map[index] = 0;cout << "succeed to free memory: " << p << endl;}}else{free(p);}}} };unsigned int Test::c_count = 0; char* Test::c_buffer = NULL; char* Test::c_map = NULL;int main(int argc, char *argv[]) {char buffer[12] = {0};Test::SetMemorySource(buffer, sizeof(buffer));cout << "===== Test Single Object =====" << endl;Test* pt = new Test;delete pt;cout << "===== Test Object Array =====" << endl;Test* pa[5] = {0};for(int i=0; i<5; i++){pa[i] = new Test;cout << "pa[" << i << "] = " << pa[i] << endl;}for(int i=0; i<5; i++){cout << "delete " << pa[i] << endl;delete pa[i];}return 0; }

2 new[]和delete[]操作符重載

new[]/delete[]和new/delete完全不同:

  • 動態對象數組創建通過new[]完成。
  • 動態對象數組的銷毀通過delete[]完成。
  • new[]/delete[]能夠被重載,進而改變內存管理方式。

new[]/delete[]的重載方式:

注意事項:

  • new[]實際需要返回的內存空間可能比期望的要多。
  • 對象數組占用的內存中需要保存數組信息。
  • 數組信息用于確定構造函數和析構函數的調用次數。

編程實驗:動態數組的內存管理

#include <iostream> #include <string> #include <cstdlib>using namespace std;class Test {int m_value; public:Test(){m_value = 0;}~Test(){}void* operator new (unsigned int size){cout << "operator new: " << size << endl;return malloc(size);}void operator delete (void* p){cout << "operator delete: " << p << endl;free(p);}void* operator new[] (unsigned int size){cout << "operator new[]: " << size << endl;return malloc(size);}void operator delete[] (void* p){cout << "operator delete[]: " << p << endl;free(p);} };int main(int argc, char *argv[]) {Test* pt = NULL;pt = new Test;delete pt;pt = new Test[5];delete[] pt;return 0; }

參考資料:

  • C++深度解析教程
  • 總結

    以上是生活随笔為你收集整理的C++中的new和delete操作符重载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。