C++中动态内存管理

来源:互联网 发布:做淘宝赚钱吗怎么做 编辑:程序博客网 时间:2024/06/16 08:49

本文总结C++ 中的内存管理,总结如下

【1】尽量用share_ptr管理资源(Efective C++条款13:以对象管理资源)

shared_ptr (C++ Primer)自动销毁所管理的对象:            有层含义:            1、一个shared_ptr的实例引用计数为0时,shared_ptr类自动销毁此对象。            { ...              shared_ptr<int> sp(new(10))  //sp 指向一个int 型,值为10的地址。            ...            }  //sp 离开此作用域时,sp本身会被销毁            2、调用所指对象的析构函数,执行资源回收。(注意:默认析构函数也会进行资源回收,但是他只回收栈空间,不回收堆空间)            class Son{            public:                int x;                int y;            Son(int x_, int y_){                cout << "constructor of son"<< endl;                this->x = x_;                this->y = y_;            }            ~Son(){cout << "dis con of Son"<< endl;}            };            class Base{            public:                int a ;                Son * point;   //point 本身在栈空间,但是它指向的内存在堆空间                Base(int a_, int x_, int y_){                point = new Son(x_, y_);                this->a = a_;                cout << "constructor of Base" << endl;            }            ~Base(){            cout << "dis con of Base" << endl;            delete point;  // 默认构造函数仅仅析构栈空间,因此得手动析构堆空间            point = NULL;}            };            // main code            shared_ptr<Base> bp(new Base(1,2,3));            //在此语句执行时候,首先会执行Base的构造函数,然后执行过程中遇到point = new Son(x_, y_);转去执行Son构造函数。            //在离开作用域后,会自动调用Base的析构函数。在执行过程中,遇到delete point;转去执行Son析构函数,释放掉Point指向的内存。然后继续执行point = NULL;(防止野指针)

【2】完整的程序 ,在类中不使用shared_ptr,因此需要在析构函数中手动完成内存释放与指针重置。

#include <iostream>#include <memory>using namespace std;class Son{public:    int x;    int y;    Son(int x_, int y_){        cout << "constructor of son"<< endl;        this->x = x_;        this->y = y_;    }    ~Son(){        cout << "dis con of Son"<< endl;    }};class Base{public:    int a ;    Son * point;    Base(int a_, int x_, int y_){        point = new Son(x_, y_);        this->a = a_;        cout << "constructor of Base" << endl;    }    ~Base(){        cout << "dis con of Base" << endl;        delete point;        point = NULL;    }};class Func{public:    Base * base;    Func(){        base = new Base(4,5,6);        cout << "constructor of Func"<< endl;    }    ~Func(){        cout << "dis con of Func"<< endl;        delete base;        base =NULL;    }};int main(int argc, const char * argv[]) {    int * p = new int[124];  //124个int    delete  p;    p = NULL;    shared_ptr<Func> fc(new Func());    std::cout << "Hello, World!\n";    return 0;}//---------结果---------constructor of sonconstructor of Baseconstructor of FuncHello, World!dis con of Funcdis con of Basedis con of Son

【3】完整的程序 ,整体使用shared_ptr,析构函数为空。也无内存泄漏。

#include <iostream>#include <memory>using namespace std;class Son{public:    int x;    int y;    Son(int x_, int y_){        cout << "constructor of son"<< endl;        this->x = x_;        this->y = y_;    }    ~Son(){        cout << "dis con of Son"<< endl;    }};class Base{public:    int a ;    shared_ptr<Son>  point;    Base(int a_, int x_, int y_){        point.reset(new Son(x_, y_));        this->a = a_;        cout << "constructor of Base" << endl;    }    ~Base(){        cout << "dis con of Base" << endl;    }};class Func{public:    shared_ptr<Base> base;    Func(){        base.reset(new Base(4,5,6));        cout << "constructor of Func"<< endl;    }    ~Func(){        cout << "dis con of Func"<< endl;    }};int main(int argc, const char * argv[]) {    int * p = new int[124];    delete []p;    p = NULL;    shared_ptr<Func> fc(new Func());    std::cout << "Hello, World!\n";    return 0;}

【4】内存泄漏也野指针

在堆上分配空间,然后得到对应的指针。
【内存泄漏】:指针没了,内存没有释放。
【野指针】: 内存释放了,指针还指向那个内存。

/*-----------------内存泄漏--------------------*/int *p = new int[14];  //operating p *p = NULL;                 //内存泄漏了/*----------------- 野指针 -------------------*/int *p = new int[14];  //operating p delete []p;                 // p成了野指针     /*-----------------正确步骤--------------------*/int *p = new int[14];  //operating p delete []p;                 // p成了野指针    p=NULL;

【5】如何检测内存泄漏

valgrind --tool=memcheck --track-origins=yes --leak-check=full ./a.out

Ref: https://startupnextdoor.com/how-to-run-valgrind-in-clion-for-c-and-c-programs/

原创粉丝点击