• 《C++新经典》第18章 内存高级话题


    18.1 new、delete的进一步认识

    18.1.2 从new说起

    1. new对象时有无括号的区别
    class A {
    public:
    };
    
    A *pa = new A();
    A *pa2 = new A;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (1)空类,两者无区别;
    (2)若类A有成员变量(int m_i;),有括号初始化为0,无括号随机值。
    (3)若类A有构造函数(A(){}),初始化工作交给构造函数自己做(构造函数空,随机值)。

    int *p1 = new int;//随机值
    int *p2 = new int();//0
    int *p3 = new int(100);//100
    
    • 1
    • 2
    • 3
    1. new做的事情

    new关键字或者操作符,主要做了两件事。
    调用operator new()函数(内部调用了malloc()函数)和调用类的构造函数。

    A *pa = new A();	//操作数
    	operator new();	//函数
    		malloc();	//c风格函数分配内存
    	A::A();			//有构造函数就调用构造函数
    
    • 1
    • 2
    • 3
    • 4
    delete pa;
    	A::~A();			//存在析构函数,则先调用析构函数
    	operator delete();	//函数
    		free();			//c风格函数释放内存
    
    • 1
    • 2
    • 3
    • 4

    new与malloc区别:
    (1)new是关键字/操作符,malloc是函数;
    (2)new对象时,分配内存+调用构造函数(若存在);
    (3)new A(),可以成员变量初始化。

    delete与free区别:
    (1)delete是关键字/操作符,free是函数;
    (2)delete对象时,释放内存+调用析构函数(若存在);

    1. malloc做了什么事

    new最终通过调用malloc来分配内存。

    18.2 new内存分配细节探秘与重载类内operator new、delete

    18.2.1 new内存分配细节探秘

    char *p = new char[10]; //new char[10](0);
    memset(p, 0, 10);
    delete[] p;
    
    • 1
    • 2
    • 3

    malloc分配内存周围记录了很多其他内容,记录分配的字节数等。
    free内存块,包括合并数据块、登记空闲块的大小、设置空闲块首位的一些标记以方便下次分配等。

    malloc分配可能的内存布局:
    记录分配字节数(4字节)
    Debug模式下可能的调试信息(几十字节)
    实际分配字节
    其他必要信息,边界调整的字节填充等(几十字节)
    回收内存时内存尾标记的尾信息(4字节)

    18.2.2 重载类内operator new、operator delete操作符

    A *pa = new A();	//操作数
    	operator new();	//函数
    		malloc();	//c风格函数分配内存
    	A::A();			//有构造函数就调用构造函数
    
    • 1
    • 2
    • 3
    • 4

    等价于

    void *tmp = operator new(sizeof(A));
    A *pa = static_cast<A *>(tmp);
    pa->A::A();	
    
    • 1
    • 2
    • 3
    delete pa;
    	A::~A();			//存在析构函数,则先调用析构函数
    	operator delete();	//函数
    		free();			//c风格函数释放内存
    
    • 1
    • 2
    • 3
    • 4

    等价于

    pa->A::A();	
    operator delete(pa);
    
    • 1
    • 2
    class A{
    public:
    	//无static似乎也行(估计编译器内部处理)
    	static void * operator new(size_t size);
    	static void * operator delete(void *p);
    	A(){
    		cout<<"A()"<<endl;
    	}
    	~A(){
    		cout<<"~A()"<<endl;
    	}
    };
    
    void * A::operator new(size_t size){//size == sizeof(A),一个字节
    	cout<<"A::operator new()"<<endl;
    	A *p = (A *)malloc(size);
    	return p;
    }
    void * A::operator delete(void *p){
    	cout<<"A::operator delete()"<<endl;
    	free(p);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    A *pa1 = new A();
    delete pa1;
    
    //::作用域运算符
    //调用全局new和delete关键字
    A *pa2 = ::new A();
    ::delete pa2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    18.2.3 重载类内operator new[]、operator delete[]操作符

    class A{
    public:
    	//无static似乎也行(估计编译器内部处理)
    	static void * operator new[](size_t size);
    	static void * operator delete[](void *p);
    	A(){
    		cout<<"A()"<<endl;
    	}
    	~A(){
    		cout<<"~A()"<<endl;
    	}
    };
    
    //size == 7,sizeof(A) * 3 + 4
    //多出4个字节记录数组大小,知道调用构造函数和析构函数次数。
    void * A::operator new[](size_t size){
    	cout<<"A::operator new[]()"<<endl;
    	A *p = (A *)malloc(size);
    	return p;
    }
    void * A::operator delete[](void *p){
    	cout<<"A::operator delete[]()"<<endl;
    	free(p);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    A *pa1 = new A[3]();
    delete[] pa1;
    
    //输出
    /*
    A::operator new[]()
    A()
    A()
    A()
    ~A()
    ~A()
    ~A()
    A::operator delete[]()
    */ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    18.3 内存池概念、代码实现和详细分析

    18.3.1 内存池的概念和实现原理简介

    malloc频繁分配小块内存时,浪费明显。

    内存池解决的主要问题:

    • 减少malloc调用次数,可以减少内存浪费;
    • 减少malloc调用次数,稍微提升程序的运行效率或者运行速度;

    内存池的实现原理是用malloc申请一大块内存,使用时一点点分配。不够时,重新分配更大一块内存,然后一点点分配。涉及到内存如何一小块分割和回收问题。

    18.3.2 内存池实现演示代码

    
    #include 
    #include 
    #include 
    using namespace std;
    
    namespace _n1
    {
        class A
        {
        public:
            static void *operator new(size_t size);
            static void operator delete(void *p);
    
            A()
            {
                cout << "A()\n";
            }
            ~A()
            {
                cout << "~A()\n";
            }
        };
    
        void *A::operator new(size_t size)
        {
            cout << "new\n";
            //传统实现
            A *p = (A *)malloc(size);
            return p;
        }
        void A::operator delete(void *p)
        {
            cout << "delete\n";
            //传统实现
            free(p);
        }
    }
    
    namespace _n2
    {
        class A
        {
        public:
            static void *operator new(size_t size);
            static void operator delete(void *p);
            static int m_iCount;       //分配计数统计,每new一次+1
            static int m_iMallocCount; //统计malloc次数,每malloc一次+1
    
            A()
            {
                // cout << "A()\n";
            }
            ~A()
            {
                // cout << "~A()\n";
            }
    
        private:
            A *next;
            static A *m_FreePosi;     //指向一块分配出去的内存首地址
            static int m_sTrunkCount; //一次分配多少倍该类的内存
        };
    
        void *A::operator new(size_t size)
        {
            // cout << "new\n";
    
            A *tmplink;
            if (m_FreePosi == nullptr)
            {
                size_t realsize = size * m_sTrunkCount;
                m_FreePosi = reinterpret_cast<A *>(new char[realsize]);
    
                for (tmplink = m_FreePosi; tmplink != &m_FreePosi[m_sTrunkCount - 1]; ++tmplink)
                    tmplink->next = tmplink + 1;
                tmplink->next = nullptr;
    
                ++m_iMallocCount;
            }
    
            tmplink = m_FreePosi;
            m_FreePosi = m_FreePosi->next;
            ++m_iCount;
            return tmplink;
        }
        void A::operator delete(void *p)
        {
            // cout << "delete\n";
    
            (static_cast<A *>(p))->next = m_FreePosi;
            m_FreePosi = static_cast<A *>(p);
        }
    
        int A::m_iCount = 0;
        int A::m_iMallocCount = 0;
    
        A *A::m_FreePosi = nullptr;
        int A::m_sTrunkCount = 5 * 100;
    }
    
    namespace _n3
    {
        class A
        {
        public:
            static void *operator new(size_t size);
            static void operator delete(void *p);
            static int m_iCount;       //分配计数统计,每new一次+1
            static int m_iMallocCount; //统计malloc次数,每malloc一次+1
    
        private:
            A *next;
            static A *m_FreePosi;     //指向一块分配出去的内存首地址
            static int m_sTrunkCount; //一次分配多少倍该类的内存
        };
    
    //#define MYMEMPOOL
    
        void *A::operator new(size_t size)
        {
    #ifdef MYMEMPOOL
            A *p = (A *)malloc(size);
            return p;
    #endif
    
            A *tmplink;
            if (m_FreePosi == nullptr)
            {
                m_sTrunkCount *= 2;
                size_t realsize = size * m_sTrunkCount;
                m_FreePosi = reinterpret_cast<A *>(new char[realsize]);
    
                for (tmplink = m_FreePosi; tmplink != &m_FreePosi[m_sTrunkCount - 1]; ++tmplink)
                    tmplink->next = tmplink + 1;
                tmplink->next = nullptr;
    
                ++m_iMallocCount;
            }
    
            tmplink = m_FreePosi;
            m_FreePosi = m_FreePosi->next;
            ++m_iCount;
            return tmplink;
        }
    
        void A::operator delete(void *p)
        {
    #ifdef MYMEMPOOL
            free(p);
            return;
    #endif
    
            (static_cast<A *>(p))->next = m_FreePosi;
            m_FreePosi = static_cast<A *>(p);
        }
    
        int A::m_iCount = 0;
        int A::m_iMallocCount = 0;
    
        A *A::m_FreePosi = nullptr;
        int A::m_sTrunkCount = 5 * 100;
    }
    
    int main()
    {
        if (0)
        {
            _n1::A *pa = new _n1::A();
            delete pa;
            cout << sizeof(_n1::A) << endl;
        }
    
        if (0)
        {
            clock_t start = clock();
            for (int i = 0; i < 5000000; i++)
                _n2::A *pa = new _n2::A();
            // delete pa;
    
            clock_t end = clock();
    
            cout << "m_iCount: " << _n2::A::m_iCount << endl;
            cout << "m_iMallocCount: " << _n2::A::m_iMallocCount << endl;
            cout << "time: " << (end - start) / 1000.0 << endl;
        }
    
        if (1)
        {
            clock_t start = clock();
            for (int i = 0; i < 5000000; i++)
                _n3::A *pa = new _n3::A();
            clock_t end = clock();
    
            cout << "m_iCount: " << _n3::A::m_iCount << endl;
            cout << "m_iMallocCount: " << _n3::A::m_iMallocCount << endl;
            cout << "time: " << (end - start) / 1000.0 << endl;
        }
    
        cout << "Over!\n";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202

    18.3.3 内存池代码后续说明

    
    #include 
    #include 
    #include 
    using namespace std;
    
    namespace _n1
    {
        class A
        {
        public:
            static void *operator new(size_t size);
            static void operator delete(void *p);
    
            A()
            {
                cout << "A()\n";
            }
            ~A()
            {
                cout << "~A()\n";
            }
        };
    
        void *A::operator new(size_t size)
        {
            cout << "new\n";
            //传统实现
            A *p = (A *)malloc(size);
            return p;
        }
        void A::operator delete(void *p)
        {
            cout << "delete\n";
            //传统实现
            free(p);
        }
    }
    
    namespace _n2
    {
        class A
        {
        public:
            static void *operator new(size_t size);
            static void operator delete(void *p);
            static int m_iCount;       //分配计数统计,每new一次+1
            static int m_iMallocCount; //统计malloc次数,每malloc一次+1
    
            A()
            {
                // cout << "A()\n";
            }
            ~A()
            {
                // cout << "~A()\n";
            }
    
        private:
            A *next;
            static A *m_FreePosi;     //指向一块分配出去的内存首地址
            static int m_sTrunkCount; //一次分配多少倍该类的内存
        };
    
        void *A::operator new(size_t size)
        {
            // cout << "new\n";
    
            A *tmplink;
            if (m_FreePosi == nullptr)
            {
                size_t realsize = size * m_sTrunkCount;
                m_FreePosi = reinterpret_cast<A *>(new char[realsize]);
    
                for (tmplink = m_FreePosi; tmplink != &m_FreePosi[m_sTrunkCount - 1]; ++tmplink)
                    tmplink->next = tmplink + 1;
                tmplink->next = nullptr;
    
                ++m_iMallocCount;
            }
    
            tmplink = m_FreePosi;
            m_FreePosi = m_FreePosi->next;
            ++m_iCount;
            return tmplink;
        }
        void A::operator delete(void *p)
        {
            // cout << "delete\n";
    
            (static_cast<A *>(p))->next = m_FreePosi;
            m_FreePosi = static_cast<A *>(p);
        }
    
        int A::m_iCount = 0;
        int A::m_iMallocCount = 0;
    
        A *A::m_FreePosi = nullptr;
        int A::m_sTrunkCount = 5 * 100;
    }
    
    namespace _n3
    {
        class A
        {
        public:
            static void *operator new(size_t size);
            static void operator delete(void *p);
            static int m_iCount;       //分配计数统计,每new一次+1
            static int m_iMallocCount; //统计malloc次数,每malloc一次+1
    
        private:
            A *next;
            static A *m_FreePosi;     //指向一块分配出去的内存首地址
            static int m_sTrunkCount; //一次分配多少倍该类的内存
    
            //释放内存池
            class GC
            {
            public:
                ~GC()
                {
                    cout << "~GC()\n";
                    while (A::m_FreePosi)
                    {
                        A *tmp = A::m_FreePosi;
                        free((void *)A::m_FreePosi);
                        A::m_FreePosi = tmp->next;
                    }
                }
            };
            static GC gc;
        };
    
        //#define MYMEMPOOL
    
        void *A::operator new(size_t size)
        {
    #ifdef MYMEMPOOL
            A *p = (A *)malloc(size);
            return p;
    #endif
    
            A *tmplink;
            if (m_FreePosi == nullptr)
            {
                m_sTrunkCount *= 2;
                size_t realsize = size * m_sTrunkCount;
                m_FreePosi = reinterpret_cast<A *>(new char[realsize]);
    
                for (tmplink = m_FreePosi; tmplink != &m_FreePosi[m_sTrunkCount - 1]; ++tmplink)
                    tmplink->next = tmplink + 1;
                tmplink->next = nullptr;
    
                ++m_iMallocCount;
            }
    
            tmplink = m_FreePosi;
            m_FreePosi = m_FreePosi->next;
            ++m_iCount;
            return tmplink;
        }
    
        void A::operator delete(void *p)
        {
    #ifdef MYMEMPOOL
            free(p);
            return;
    #endif
    
            (static_cast<A *>(p))->next = m_FreePosi;
            m_FreePosi = static_cast<A *>(p);
        }
    
        int A::m_iCount = 0;
        int A::m_iMallocCount = 0;
    
        A *A::m_FreePosi = nullptr;
        int A::m_sTrunkCount = 5 * 100;
    
        A::GC A::gc;
    }
    
    int main()
    {
        if (0)
        {
            _n1::A *pa = new _n1::A();
            delete pa;
            cout << sizeof(_n1::A) << endl;
            //_n1::A a;
        }
    
        if (0)
        {
            clock_t start = clock();
            for (int i = 0; i < 5000000; i++)
                _n2::A *pa = new _n2::A();
            // delete pa;
    
            clock_t end = clock();
    
            cout << "m_iCount: " << _n2::A::m_iCount << endl;
            cout << "m_iMallocCount: " << _n2::A::m_iMallocCount << endl;
            cout << "time: " << (end - start) / 1000.0 << endl;
        }
    
        if (0)
        {
            clock_t start = clock();
            for (int i = 0; i < 5000000; i++)
                _n3::A *pa = new _n3::A();
            clock_t end = clock();
    
            cout << "m_iCount: " << _n3::A::m_iCount << endl;
            cout << "m_iMallocCount: " << _n3::A::m_iMallocCount << endl;
            cout << "time: " << (end - start) / 1000.0 << endl;
        }
    
        cout << "Over!\n";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222

    18.4 嵌入式指针概念及范例、内存池改进版

    18.4.1 嵌入式指针

    1. 嵌入式指针概念
      embedded pointer

    类A中存在成员变量A *next;当一个空闲内存块分配出去后,next指向无实际意义(不需要了),可以被覆盖,另作他用,节省字节。

    next占用4个字节,sizeof(A)必须大于等于4。

    1. 嵌入式指针演示代码
    
    #include 
    #include 
    #include 
    using namespace std;
    
    class TestEP
    {
    public:
        int m_i = 5;
        int m_j;
        struct obj
        {
            struct obj *next;
        };
    };
    
    int main()
    {
        cout << sizeof(TestEP) << endl;
    
        TestEP mytest;
        cout<<mytest.m_i<<endl;
    
        TestEP::obj *ptmp = (TestEP::obj *)&mytest;
        ptmp->next = nullptr;
        cout<<mytest.m_i<<endl;
    
        cout << "Over!\n";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    18.4.2 内存池代码的改进

    
    #include 
    #include 
    #include 
    using namespace std;
    
    //专门的内存池类或内存分配器
    //使用本类的类sizeof()必须大于等于4
    class myallocator
    {
    public:
        //释放内存池
        ~myallocator()
        {
            while (m_FreePosi)
            {
                obj *tmp = m_FreePosi;
                m_FreePosi = m_FreePosi->next;
                free((void *)tmp);
            }
        }
    
        //分配内存接口
        void *allocate(size_t size)
        {
            obj *tmplink;
            if (m_FreePosi == nullptr)
            {
                size_t realsize = m_sTrunkCout * size;
                m_FreePosi = (obj *)malloc(realsize);
    
                tmplink = m_FreePosi;
                for (int i = 0; i < m_sTrunkCout - 1; ++i)
                {
                    tmplink->next = (obj *)((char *)tmplink + size);
                    tmplink = tmplink->next;
                }
                tmplink->next = nullptr;
    
                m_sTrunkCout *= 2;
            }
            tmplink = m_FreePosi;
            m_FreePosi = m_FreePosi->next;
            return tmplink;
        }
        //释放内存接口
        void deallocate(void *p)
        {
            ((obj *)p)->next = m_FreePosi;
            m_FreePosi = (obj *)p;
        }
    
    private:
        //类内结构,只能类内使用
        struct obj
        {
            struct obj *next; // next就是嵌入式指针
        };
        int m_sTrunkCout = 5;
        obj *m_FreePosi = nullptr;
    };
    
    class A
    {
    public:
        //必须保证sizeof(A)凑够4字节
        int m_i;
        int m_j;
    
    public:
        static myallocator myalloc;
        static void *operator new(size_t size)
        {
            return myalloc.allocate(size);
        }
        static void operator delete(void *p)
        {
            myalloc.deallocate(p);
        }
    };
    myallocator A::myalloc;
    
    int main()
    {
        {
            cout << sizeof(myallocator) << endl;
            cout << sizeof(A) << endl;
    
            A *mypa[100];
            for (int i = 0; i < 15; ++i)
            {
                mypa[i] = new A();
                printf("%p\n", mypa[i]);
            }
            for (int i = 0; i < 15; ++i)
            {
                delete mypa[i];
            }
        }
    
        cout << "Over!\n";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    
    #include 
    #include 
    #include 
    using namespace std;
    
    //专门的内存池类或内存分配器
    //使用本类的类sizeof()必须大于等于4
    class myallocator
    {
    public:
        //释放内存池
        ~myallocator()
        {
            while (m_FreePosi)
            {
                obj *tmp = m_FreePosi;
                m_FreePosi = m_FreePosi->next;
                free((void *)tmp);
            }
        }
    
        //分配内存接口
        void *allocate(size_t size)
        {
            obj *tmplink;
            if (m_FreePosi == nullptr)
            {
                size_t realsize = m_sTrunkCout * size;
                m_FreePosi = (obj *)malloc(realsize);
    
                tmplink = m_FreePosi;
                for (int i = 0; i < m_sTrunkCout - 1; ++i)
                {
                    tmplink->next = (obj *)((char *)tmplink + size);
                    tmplink = tmplink->next;
                }
                tmplink->next = nullptr;
    
                m_sTrunkCout *= 2;
            }
            tmplink = m_FreePosi;
            m_FreePosi = m_FreePosi->next;
            return tmplink;
        }
        //释放内存接口
        void deallocate(void *p)
        {
            ((obj *)p)->next = m_FreePosi;
            m_FreePosi = (obj *)p;
        }
    
    private:
        //类内结构,只能类内使用
        struct obj
        {
            struct obj *next; // next就是嵌入式指针
        };
        int m_sTrunkCout = 5;
        obj *m_FreePosi = nullptr;
    };
    
    #define DECLARE_POOL_ALLOC()               \
    public:                                    \
        static void *operator new(size_t size) \
        {                                      \
            return myalloc.allocate(size);     \
        }                                      \
        static void operator delete(void *p)   \
        {                                      \
            myalloc.deallocate(p);             \
        }                                      \
        static myallocator myalloc;
    
    #define IMPLEMENT_POOL_ALLOC(classname) \
        myallocator classname::myalloc;
    
    class A
    {
        DECLARE_POOL_ALLOC();
    
    public:
        //必须保证sizeof(A)凑够4字节
        int m_i;
        int m_j;
    };
    IMPLEMENT_POOL_ALLOC(A);
    
    int main()
    {
        {
            cout << sizeof(myallocator) << endl;
            cout << sizeof(A) << endl;
    
            A *mypa[100];
            for (int i = 0; i < 15; ++i)
            {
                mypa[i] = new A();
                printf("%p\n", mypa[i]);
            }
            for (int i = 0; i < 15; ++i)
            {
                delete mypa[i];
            }
        }
    
        cout << "Over!\n";
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109

    18.5 重载全局new/delete、定位new及重载

    18.5.1 重载全局operator new/delete ([]) 操作符

    //重载全局operator new
    void * operator new(size_t size){
    	return malloc(size);
    }
    
    //重载全局operator new[]
    void * operator new[](size_t size){
    	return malloc(size);
    }
    
    //重载全局operator delete
    void * operator delete(void * p){
    	return free(p);
    }
    
    //重载全局operator delete[]
    void * operator delete[](void * p){
    	return free(p);
    }
    
    int *pi = new int(12);
    delete pi;
    char *pc = new char[10](0);
    delete[] pc;
    
    A *p = new A();
    delete p;
    A *pa = new A[3]();
    delete[] pa;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    一般重载类中operator new/delete ([])。

    18.5.2 定位new(placement new)

    placement new无对应的placement delete,在已经分配的原始内存中初始化一个对象。

    • 定位new不分配内存,使用定位new前内存必须先分配好;
    • 初始化对象,就是调用对象的构造函数。
    new(地址) 类类型(参数);
    
    • 1
    class PLA{
    public:
    	int m_a;
    	PLA():m_a(0){
    		cout<<"PLA()"<<endl;
    	}
    	PLA(int tmp):m_a(tmp){
    		cout<<"PLA(int)"<<endl;
    	}
    	~PLA()
    		cout<<"~PLA()"<<endl;
    	}
    };
    
    //先分配内存
    void *p1 = (void *)new char[sizeof(PLA)];
    //定位new,调用无参构造函数,不额外分配内存
    PLA *po1 = new(p1) PLA();
    po1->~PLA();//根据需要调用析构函数
    delete[](void *)po1;//释放内存
    //delete[] p1;//同上
    
    
    //先分配内存
    void *p2 = (void *)new char[sizeof(PLA)](0);
    //定位new,调用有参构造函数,不额外分配内存
    PLA *po2 = new(p2) PLA(12);
    po2->~PLA();//根据需要调用析构函数
    delete[](void *)po2;//释放内存
    //delete[] p2;//同上
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    定位new调用关系

    PLA *pa = new(已经分配好的内存首地址) PLA();//定位new操作符
    	operator new();//函数,内部没有调用malloc
    	PLA::PLA();//调用构造函数
    
    • 1
    • 2
    • 3

    定位new调用的operator new操作符的重载代码。

    public:
    	void * operator new(size_t, void *p){//多一个p,指向已经分配好的内存首地址
    		return p;
    	}
    
    • 1
    • 2
    • 3
    • 4

    18.5.3 多种版本的operator new 重载

    第一个参数固定size_t(sizeof(对象)),其他参数指定。

    //无内存分配,无类构造函数调用
    PLA *po2 = new(1234, 56) PLA(12);
    
    public:
    	//第一个参数,系统默认传递sizeof(PLA)
    	void * operator new(size_t size, int t1, int t2){
    		return NULL;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    会出现警告:void *PLA::operator new(size_t, int, int)未找到匹配的删除运算符。

    可以增加对应的operator delete重载以避免警告(非必需)。

    public:
    	void  operator delete(void* p, int t1, int t2){
    		return;
    	}
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    【CSS】设置文本样式
    电机模型预测控制算法
    2023华为HCIA+HCIP最全Datacom题库解析
    DPDK中的cuckoo hash算法
    Spring MVC框架
    Docker配置双栈网络引起的IPV6不通问题排查及解决
    自动化测试的生命周期是什么?
    【腾讯云 HAI域探秘】高性能服务器引领AI革新浪潮:从AI绘画、知识问答到PyTorch图像分类、视频检测的全方位探索
    Android WebView专题
    R语言中的数据重塑
  • 原文地址:https://blog.csdn.net/oqqyx1234567/article/details/127316584