malloc与new,free与delete
生活随笔
收集整理的這篇文章主要介紹了
malloc与new,free与delete
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
malloc,free是一種庫函數(shù),不能被重載,new和delete是運(yùn)算符,可以被重載
new和delete內(nèi)部都調(diào)用malloc和free函數(shù)
new的三種用法: 1.new,不可以被重載直接new一個(gè)對(duì)象T* ptr = new T();new一個(gè)對(duì)象時(shí)做了兩件事情 * 調(diào)用了operator new申請(qǐng)空間 * 調(diào)用了構(gòu)造函數(shù),如果括號(hào)中為空,會(huì)調(diào)用默認(rèn)構(gòu)造函數(shù)2.operator new,可以被重載重載形式:必須為void* operator new(size_t n, . . .); 行為類似于malloc,就是申請(qǐng)一段空間重載后new一個(gè)對(duì)象會(huì)調(diào)用這個(gè)函數(shù)3.placement new原地構(gòu)造一個(gè)對(duì)象,相當(dāng)于顯示調(diào)用構(gòu)造函數(shù)new (T*) T(args); 使用方式如下:A *pointer = (A*)malloc(sizeof(A));new(pointer)A(10);cout << pointer->i << endl;
delete:類似于new 會(huì)首先調(diào)用析構(gòu)函數(shù)析構(gòu),然后調(diào)用free()釋放空間 可以被重載,重載形式如下:void operator delete(void* ptr, . . .);
new[]: 首先調(diào)用operator new[]申請(qǐng)n段連續(xù)的T空間,然后會(huì)調(diào)用n次默認(rèn)構(gòu)造函數(shù),可以被重載 重載形式如下:void* operator new[](size_t n, . . .); 如果重載了此函數(shù),new[]會(huì)調(diào)用這個(gè)函數(shù)
delete[]: 與new[]配套使用,會(huì)先調(diào)用n次析構(gòu)函數(shù),然后再釋放空間,可以被重載 重載形式如下:void operator delete[](void* ptr, . . .); 如果重載了此函數(shù),delete[]會(huì)調(diào)用這個(gè)函數(shù)。
測(cè)試代碼:
#include <iostream> using namespace std; class A { public:A() : i(1) { cout << "A default constructor" << endl; }A(int j) : i(j) { cout << "A constructor" << endl; }~A() { cout << "A destructor" << endl; }int i; };class B { public:B() : i(1) { cout << "B default constructor" << endl; }B(int j) : i(j) { cout << "B constructor" << endl; }~B() { cout << "B destructor" << endl; }void* operator new(size_t n){cout << "B my new" << endl;B* p = (B*)malloc(n);return p;}void* operator new[](size_t n){cout << "B my new[]" << endl;B* p = (B*)malloc(n * sizeof(B));return p;}void operator delete(void* ptr){cout << "B my delete" << endl;free(ptr);}void operator delete[](void* ptr){cout << "B my delete[]" << endl;free(ptr);}int i; };int main() {//---------------------------A---------------------------// A *ptr_A_1 = new A;cout << ptr_A_1->i << endl;delete ptr_A_1;cout << endl;A *ptr_A_2 = new A();cout << ptr_A_2->i << endl;delete ptr_A_2;cout << endl;A *ptr_A_3 = new A(2);cout << ptr_A_3->i << endl;delete ptr_A_3;cout << endl;A *ptr_A_4 = new A[3];auto p = ptr_A_4;for(int i = 0; i < 3; ++i) cout << (p + i)->i << " ";cout << endl;delete[] ptr_A_4;cout << endl;//------------------------B-------------------------// B *ptr_B_1 = new B;cout << ptr_B_1->i << endl;delete ptr_B_1;cout << endl;B *ptr_B_2 = new B();cout << ptr_B_2->i << endl;delete ptr_B_2;cout << endl;B *ptr_B_3 = new B(2);cout << ptr_B_3->i << endl;delete ptr_B_3;cout << endl;B *ptr_B_4 = new B[3];auto p1 = ptr_B_4;for(int i = 0; i < 3; ++i) cout << (p1 + i)->i << " ";cout << endl;delete[] ptr_B_4;cout << endl;return 0; }
new的三種用法: 1.new,不可以被重載直接new一個(gè)對(duì)象T* ptr = new T();new一個(gè)對(duì)象時(shí)做了兩件事情 * 調(diào)用了operator new申請(qǐng)空間 * 調(diào)用了構(gòu)造函數(shù),如果括號(hào)中為空,會(huì)調(diào)用默認(rèn)構(gòu)造函數(shù)2.operator new,可以被重載重載形式:必須為void* operator new(size_t n, . . .); 行為類似于malloc,就是申請(qǐng)一段空間重載后new一個(gè)對(duì)象會(huì)調(diào)用這個(gè)函數(shù)3.placement new原地構(gòu)造一個(gè)對(duì)象,相當(dāng)于顯示調(diào)用構(gòu)造函數(shù)new (T*) T(args); 使用方式如下:A *pointer = (A*)malloc(sizeof(A));new(pointer)A(10);cout << pointer->i << endl;
delete:類似于new 會(huì)首先調(diào)用析構(gòu)函數(shù)析構(gòu),然后調(diào)用free()釋放空間 可以被重載,重載形式如下:void operator delete(void* ptr, . . .);
new[]: 首先調(diào)用operator new[]申請(qǐng)n段連續(xù)的T空間,然后會(huì)調(diào)用n次默認(rèn)構(gòu)造函數(shù),可以被重載 重載形式如下:void* operator new[](size_t n, . . .); 如果重載了此函數(shù),new[]會(huì)調(diào)用這個(gè)函數(shù)
delete[]: 與new[]配套使用,會(huì)先調(diào)用n次析構(gòu)函數(shù),然后再釋放空間,可以被重載 重載形式如下:void operator delete[](void* ptr, . . .); 如果重載了此函數(shù),delete[]會(huì)調(diào)用這個(gè)函數(shù)。
測(cè)試代碼:
#include <iostream> using namespace std; class A { public:A() : i(1) { cout << "A default constructor" << endl; }A(int j) : i(j) { cout << "A constructor" << endl; }~A() { cout << "A destructor" << endl; }int i; };class B { public:B() : i(1) { cout << "B default constructor" << endl; }B(int j) : i(j) { cout << "B constructor" << endl; }~B() { cout << "B destructor" << endl; }void* operator new(size_t n){cout << "B my new" << endl;B* p = (B*)malloc(n);return p;}void* operator new[](size_t n){cout << "B my new[]" << endl;B* p = (B*)malloc(n * sizeof(B));return p;}void operator delete(void* ptr){cout << "B my delete" << endl;free(ptr);}void operator delete[](void* ptr){cout << "B my delete[]" << endl;free(ptr);}int i; };int main() {//---------------------------A---------------------------// A *ptr_A_1 = new A;cout << ptr_A_1->i << endl;delete ptr_A_1;cout << endl;A *ptr_A_2 = new A();cout << ptr_A_2->i << endl;delete ptr_A_2;cout << endl;A *ptr_A_3 = new A(2);cout << ptr_A_3->i << endl;delete ptr_A_3;cout << endl;A *ptr_A_4 = new A[3];auto p = ptr_A_4;for(int i = 0; i < 3; ++i) cout << (p + i)->i << " ";cout << endl;delete[] ptr_A_4;cout << endl;//------------------------B-------------------------// B *ptr_B_1 = new B;cout << ptr_B_1->i << endl;delete ptr_B_1;cout << endl;B *ptr_B_2 = new B();cout << ptr_B_2->i << endl;delete ptr_B_2;cout << endl;B *ptr_B_3 = new B(2);cout << ptr_B_3->i << endl;delete ptr_B_3;cout << endl;B *ptr_B_4 = new B[3];auto p1 = ptr_B_4;for(int i = 0; i < 3; ++i) cout << (p1 + i)->i << " ";cout << endl;delete[] ptr_B_4;cout << endl;return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/CoderZSL/p/8056656.html
總結(jié)
以上是生活随笔為你收集整理的malloc与new,free与delete的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装MariaDB和Apache
- 下一篇: python之metaclass