通過從全局和類內部重載operator new /delete來獲取內存管理權

目錄

  • 1、通過重載獲得內存管理權
  • 2、容器的內存管理
  • 3、重載new、array new、replacement new,接管內存控制權
    • 1、重載全局::operator new / ::operator delete以及array版本
    • 2、在類里面去重載

1、通過重載獲得內存管理權

之前的幾章學習,是紅色的路線。此時內存管理權限不在我們手中。
接下來,通過class內部的重載operator new/delete,我們可以獲得內存管理權力。
注意operator new/delete,這張PPT里面有兩個。一個是在class內部重載,還有一個是全局的重載。
在這里插入圖片描述

2、容器的內存管理

當我們將元素放入容器中,容器顯然也要做出與上面相似的內存分配的動作。
容器講構造和析構函數包裝在construct()與destroy()中。內存的分配和釋放動作拉入分配器中。
分配器在分配內存的時候與上圖的紅色線路是一樣的。
分配器所做的就是一些復雜的內存管理。
在這里插入圖片描述

3、重載new、array new、replacement new,接管內存控制權

已知,我們可以從全局或者類里面去重載。

1、重載全局::operator new / ::operator delete以及array版本

注意這里的重載函數相對于原來的函數只是多了一個打印信息操作。
這里只是做一個接口的示范規格。

void* myAlloc(size_t size)
{return malloc(size);
}
void myFree(void* ptr)
{return free(ptr);
}他們不能被聲明于一個namespace內
inline void* operator new(size_t size)
{cout << "global new()" << endl;return myAlloc(size);
}inline void operator delete(void* ptr)
{cout << "global delete()" << endl;myFree(ptr);
}inline void* operator new[](size_t size)
{cout << "global new[]()" << endl;return myAlloc(size);
}
inline void operator delete[](void* ptr)
{cout << "global delete[]()" << endl;myFree();
}

2、在類里面去重載

我們在類里面寫好兩個重載函數:

class Foo {
public:void* operator new(size_t);void operator delete(void*,size_t);
};

注意上面的delete函數的第二個參數size_t是可有可無的。
同時也需要注意,這里的operator new和 operator delete 函數是靜態的,要加上static。因為在new一個對象的時候我們手上是沒有對象的,也就是說無法通過對象調用一般的函數。加上static就可以不通過對象而直接調用這兩個函數了。這是一個細節。
在我們進行new和delete操作時,由編譯器調用:

Foo* p = new Foo;
...
delete p;

編譯器將上述代碼轉換為如下格式:


try {void* mem = operator new(sizeof(Foo));p = static_cast<Foo*>(mem);//調用構造函數p->Foo::Foo();
}//調用析構函數
p->~Free();
operator delete(p);

同理array的版本如下:

Foo* p = new Foo[N];
...
delete [] p;class Foo {
public:void* operator new[](size_t);void operator delete[](void*,size_t);
};

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377372.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377372.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377372.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

sml完整形式_教資會的完整形式是什么?

sml完整形式教資會&#xff1a;大學教育資助委員會 (UGC: University Grants Commission) UGC is an abbreviation of the University Grants Commission. It is an organization established by the Indian Union government in agreement with the UGC Act 1956 under the Mi…

ASP.NET線程相關配置

1、ASP.NET 同一時刻只能發起的工作線程數量&#xff1a; (maxWorkerThreads * CPU邏輯數量&#xff09;-minFreeThreads 比如2個CPU默認配置maxWorkerThreads100&#xff0c;minFreeThreads176&#xff0c;則同時最大只能有24個工作線程。&#xff08;這里不管 <system.ne…

Android 編程下 AlarmManager

對應 AlarmManager 有一個 AlarmManagerServie 服務程序&#xff0c;該服務程序才是正真提供鬧鈴服務的&#xff0c;它主要維護應用程序注冊的各類鬧鈴并適時的設置即將觸發的鬧鈴給鬧鈴設備 ( 在系統中&#xff0c;Linux 實現的設備名為 ”/dev/alarm” ) &#xff0c;并且一直…

erp開發模式_ERP的完整形式是什么?

erp開發模式ERP&#xff1a;企業資源計劃 (ERP: Enterprise Resource Planning) ERP is an abbreviation of Enterprise Resource Planning. It is incorporated business management that is executed by a lot of numerous business houses to enhance their productivity an…

關于placement new 和 placement delete的重載,以及basic_string重載new()實例

關于placement new 在https://blog.csdn.net/qq_42604176/article/details/111997397中已經介紹了placement new的形式。 它的形式為new()/delete().我們將分配好內存的指針送入括號中&#xff0c;就完成了初步的調用了。 其實我們可以定義放任何的東西到()內部。只放一個指針…

cwc云萊特鏈_CWC的完整形式是什么?

cwc云萊特鏈CWC&#xff1a;工作條件的改變 (CWC: Change in Working Conditions) CWC is an abbreviation of "Change in Working Conditions". CWC是“工作條件更改”的縮寫 。 It is an expression, which is commonly used in the Gmail platform. In a particu…

在eclipse中創建web項目(非myeclipse)

在eclipse中創建web項目(非myeclipse) 在eclipse中如何創建dynamic web project項目 本文的演示是從本地文件創建dynamic web project&#xff0c;從svn檢出的同時創建dynamic web project于此類似。我們推薦使用解壓版的tomcat6.x版本&#xff0c;來作為服務器。可以到http://…

opengl glut 編譯

新建工程glut dll工程&#xff0c;本來想創建lib,工程的&#xff0c;但是想起來&#xff0c;gl是狀態機機制。dll方便資源共享等。 添加兩個include目錄 各種手機電腦平臺&#xff0c;網絡多媒體開發,mmsplayer&#xff0c;QQ514540005 然后將目錄下的lib/glut下面所有的.c文件…

劃分數據集代碼(按照4:1的比例)以及根據各自文件名寫入txt文件

會將圖片分到兩個文件夾中&#xff1a; #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #incl…

bpo是什么意思_BPO的完整形式是什么?

bpo是什么意思BPO&#xff1a;業務流程外包 (BPO: Business Process Outsourcing) BPO is an abbreviation of Business process outsourcing. It is a convention of a company to another company which is an external provider of services or business operations process…

在進行 ASP.NET 開發時,有時候需要對頁面輸出的最終 HTML 源代碼進行控制

在進行 ASP.NET 開發時&#xff0c;有時候需要對頁面輸出的最終 HTML 源代碼進行控制&#xff0c;是頁面的 render 方法中很容易實現這個功能。下面就是一個實現的方法&#xff0c;注釋都在代碼中。 [c-sharp] view plaincopy <% Page Language"C#" %> <% …

FireFox插件SQLite Manager的使用

最近幾天開始高IOS數據庫來著&#xff0c;一開始就CoreData的學習&#xff0c;結果高了一天沒有一點進展。 沒法&#xff0c;還是先老實代碼著吧&#xff0c;不過用的火狐插件可視化數據庫的操作也是不錯的似乎。 網上搜了搜用法&#xff0c;還真沒找到有什么的&#xff0c;最后…

簡單的數據增強代碼(C++與opencv)

包括了圖片批量平移、旋轉、以及像素變換 #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #in…

aes模式_AES的完整形式是什么?

aes模式AES&#xff1a;高級加密標準 (AES: Advanced Encryption Standard) AES is an abbreviation of Advanced Encryption Standard, also known by its original name Rijndael. It is an arrangement of standard for the encryption of electronic data set up by the U.…

IOS ----UIButton用法詳解

這段代碼動態的創建了一個UIButton,并且把相關常用的屬性都列舉了.希望對大家有用. //這里創建一個圓角矩形的按鈕UIButton *button1 [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能夠定義的button類型有以下6種&#xff0c;// typedef enum {// UIButtonTypeCusto…

針對一個class寫出它的內存管理池以及總結出allocator類(三個版本)

目錄示例版本1&#xff1a;per-class allocator,1示例版本2&#xff1a;per-class allocator,2最終版本&#xff1a;static allocator針對版本三進行macro如果我們不針對對象做內存管理&#xff0c;那么我們每次進行Foo* p new Foo(x);時總是會調用malloc函數。 盡管malloc函數…

kotlin 第一個程序_Kotlin程序添加兩個矩陣

kotlin 第一個程序Given two matrices, we have to add them. 給定兩個矩陣&#xff0c;我們必須將它們相加。 Example: 例&#xff1a; Input:matrix 1:[2, 3][4, 5][7, 1]matrix 2:[4, 6][9, 0][7, 6]Output:[6, 9][13, 5][14, 7] 在Kotlin中添加兩個矩陣的程序 (Progra…

ubuntu 切換用戶的命令[shell, linux]

使用ubuntu過程中免不了和shell(終端)打交道, 也不可避免在各種用戶之間進行切換, 從而實現對各帳戶的管理, 這個就涉及到了一個比較基礎又很重要的工作,怎么樣切換用戶, 對于LINUX老鳥來說,這個根本不值不提的東東卻讓新手撓頭不已, 現在給出普通用戶和超級用戶切換的命令(附圖…

曲苑雜壇--修改數據庫名和文件組名

/* 該腳本示例如何完整的修改一個數據庫的名稱. 數據庫為原名稱為DB_BEIJING&#xff0c;需要修改成DB_SHANGHAI nzperfect 2012.12.19 */--判斷是否存在同名的數據庫&#xff0c;以防止誤刪除 USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name NDB_BEIJI…

關于new handler與default、delete關鍵字

在https://blog.csdn.net/qq_42604176/article/details/111638568的operate_new源代碼長啥樣中談到過new handler。 當operator new不能夠分配出申請的內存時&#xff0c;會拋出bad_alloc 異常。有的編譯器會返回0. 當定義成new(nothrow) Foo&#xff1b;就不會拋異常&#xff…