C++中malloc/free和new/delete的区别---补充(15)《Effective C++》

本文探讨了C++中malloc/free与new/delete的区别,包括它们如何分配和释放内存、构造及析构函数的执行时机等关键信息。

1、C++中既然有了new/delete为什么还有还存在malloc/free呢?

1)malloc/free作为C/C++语言中的库函数,而new/delete是C++中的运算符而已,因此C++编译器可以强制使new/delete运算符进行构造函数和析构函数的执行,而针对库函数,C++编译器则没有那么高的权限;
2)new/delete中需要调用C语言中的一些函数,而在C语言中只可以使用malloc和free进行内存空间的申请与释放,因此,我们需要在C++中保留malloc/free函数。

2、malloc/free函数和new/delete运算符需要什么解析方式呢?

1、malloc/free

#include <iostream>
#include <cstdlib>
using namespace std;

class Base1{
public:
    int i;
    int *l;
    Base1(){
        this->i = 10;
        l = &i;
    }
    void setArgs(int i){
        this->i = i;
        l = &this->i;
    }
    Base1(int i) :i(i=10){

    }
    friend ostream& operator<<(ostream& os, Base1& base1){
        os << base1.i << endl;
        os << base1.l << endl;
        return os;
    }
    void show(){
        cout << "Base1:" << this->i << endl;
    }
    ~Base1(){
        cout << "Base1析构开始。。。" << endl;
        delete l;
    }
};
int main(){
    Base1* base = (Base1*)malloc(sizeof(Base1));
    base->setArgs(100);

    free(base);

    if (base == NULL){
        cout << "指针为空" << endl;
    }
    else{
        cout << "base指针的地址:" << base << endl;
        cout << "执行free()之后继续访问该内存空间:" << endl;
        cout << *base << endl;
    }

    /*Base1* base1 = new Base1(10);
    base1->show();
    delete base1;
    return 0;*/
}

运行结果:

这里写图片描述

调试过程:

1)下面我们通过调试这段代码了解malloc/free函数的执行机制。
这里写图片描述
2)执行free(base)之后的空间:
这里写图片描述
我们惊喜的发现,在执行free()函数后,仍旧可以通过指针访问原内存空间,所以我们认为free()操作的释放内存空间的解释:只是里面的值都被销毁掉而已,但里面的内存结构不会受到损伤,然而内部的内存结构却都完好着无损,因此,编译器随机为这些变量进行随机赋值操作,所以就导致仍旧可以通过原指针访问一段已经被我们舍弃的内存空间,可以通过原指针操纵这段内存空间,因此可能造成内存泄露的问题。

2、new/delete

#include <iostream>
#include <cstdlib>
using namespace std;

class Base1{
public:
    int i;
    int *l;
    Base1(){
        this->i = 10;
        l = &i;
    }
    void setArgs(int i){
        this->i = i;
        l = &this->i;
    }
    Base1(int i) :i(i=10){

    }
    friend ostream& operator<<(ostream& os, Base1& base1){
        os << base1.i << endl;
        os << base1.l << endl;
        return os;
    }
    void show(){
        cout << "Base1:" << this->i << endl;
    }
    ~Base1(){
        cout << "Base1析构开始。。。" << endl;
        delete l;
    }
};
int main(){
    /*Base1* base = (Base1*)malloc(sizeof(Base1));
    base->setArgs(100);

    free(base);

    if (base == NULL){
        cout << "指针为空" << endl;
    }
    else{
        cout << "base指针的地址:" << base << endl;
        cout << "执行free()之后继续访问该内存空间:" << endl;
        cout << *base << endl;
    }*/

    Base1* base1 = new Base1(10);
    base1->show();
    delete base1;
    return 0;
}

运行结果:
这里写图片描述
调试动作:

  • 执行完new动作之后的内存状况:

这里写图片描述

  • 执行完delete动作之后的内存状况:

这里写图片描述

3、malloc/free函数和new/delete的标准区别:

malloc和free只是从内存空间中申请内存和释放内存而已,而new/delete不仅从内存空间中申请释放内存,同时还会执行对象的构造函数和析构函数,即new在申请内存后执行对象创建动作,delete在释放内存的同时还会执行对象的清理动作,更加完善了,不是吗?

简单区别malloc/free和new/delete机制:

  • malloc和new的区别:

malloc只是简单的创建空间,就像刚买了自己心爱的房屋而已,然后没有任何操作;new呢?则是在买房间的同时就已经将里面装修了,并放了你喜欢的家具进去啦!!!是不是很人性化呀!

  • free和delete的区别:

free只是简单的将把房间中的东西都销毁掉而已,原来的房屋结构不会受到任何影响,即原来是三室一厅,在你搬出房屋后还是三室一厅,程序结束时候这段空间被释放,可以理解为房屋过了80年或者更久需要统一拆迁,然后被拆,但是在这段时间内,有人可能拿着房间的钥匙进入房间甚至将自己的东西放进去,然而你并不知道,这对你来说是不允许的吧!对程序来说就是可能造成内存泄漏啦!
delete呢?则是在你将房间中的东西销毁掉的同时房子也一起拆了,比较干净,这样就不会有人可以住进去啦!

PS:
建议:希望大家在C++中针对自定义类型时候一定请使用new/delete运算符动作,避免出现内存泄漏等严重问题。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值