📜  C++中的new vs malloc()和free()vs delete

📅  最后修改于: 2021-05-20 05:38:03             🧑  作者: Mango

我们在C++中使用new和delete运算符来动态分配内存,而在C和C++中,malloc()和free()函数也用于相同的目的。 new或malloc()以及delete或free()的功能似乎相同,但是它们在各种方式上有所不同。
关于构造函数和析构函数调用的行为在以下方面有所不同:
malloc()与new():

  • malloc():这是一个C库函数,也可以在C++中使用,而“ new”运算符仅适用于C++。
  • malloc()new均用于在堆中动态分配内存。但是“ new”确实调用了一个类的构造函数,而“ malloc()”却没有。

下面是说明new和malloc()功能的程序:

CPP
// C++ program to illustrate malloc()
// and new operator in C++
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Create an object of class A
    // using new operator
    A* a = new A;
    cout << "Object of class A was "
         << "created using new operator!"
         << endl;
 
    // Create an object of class A
    // using malloc operator
    A* b = (A*)malloc(sizeof(A));
    cout << "Object of class A was "
         << "created using malloc()!"
         << endl;
 
    return 0;
}


CPP
// C++ program to illustrate free()
// and delete keyword in C++
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Create an object of class A
    // using new operator
    A* a = new A;
    cout << "Object of class A was "
         << "created using new operator!"
         << endl;
 
    delete (a);
    cout << "Object of class A was "
         << "deleted using delete keyword!"
         << endl;
 
    cout << endl;
 
    A* b = (A*)malloc(sizeof(A));
    cout << "Object of class A was "
         << "created using malloc()!"
         << endl;
 
    free(b);
    cout << "Object of class A was "
         << "deleted using free()!"
         << endl;
 
    return 0;
}


CPP
// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Object Created of class A
    A a;
    return 0;
}


CPP
// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Object Created of class A
    A a;
    exit(0);
}


CPP
// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Object Created of class A
    A *a = new A;
    return 0;
}


输出:
Constructor was Called!
Object of class A was created using new operator!
Object of class A was created using malloc()!

在上面的程序中,我们可以清楚地看到,在使用新运算符Default Constructor创建对象时,未使用malloc函数Default Constructor进行调用。
free()与delete:

  • free()是一个C库函数,也可以在C++中使用,而“ delete”是一个C++关键字。
  • free()释放内存,但不调用类的Destructor,而“ delete”释放内存,还调用类的Destructor。

下面是说明new和malloc()功能的程序:

CPP

// C++ program to illustrate free()
// and delete keyword in C++
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Create an object of class A
    // using new operator
    A* a = new A;
    cout << "Object of class A was "
         << "created using new operator!"
         << endl;
 
    delete (a);
    cout << "Object of class A was "
         << "deleted using delete keyword!"
         << endl;
 
    cout << endl;
 
    A* b = (A*)malloc(sizeof(A));
    cout << "Object of class A was "
         << "created using malloc()!"
         << endl;
 
    free(b);
    cout << "Object of class A was "
         << "deleted using free()!"
         << endl;
 
    return 0;
}
输出:
Constructor was Called!
Object of class A was created using new operator!
Destructor was Called!
Object of class A was deleted using delete keyword!

Object of class A was created using malloc()!
Object of class A was deleted using free()!

以下是更多插图的程序:
程序1:

CPP

// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Object Created of class A
    A a;
    return 0;
}
输出:
Constructor was Called!
Destructor was Called!

在上面的程序中,即使不使用delete运算符,析构函数仍会被调用。析构函数调用的原因是语句“ return 0” 。在主函数执行该语句时,将调用为其创建对象的每个类的析构函数。
为了避免析构函数调用,我们可以将语句“ return 0”替换为“ exit(0)”。下面是相同的代码:
程式2:

CPP

// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Object Created of class A
    A a;
    exit(0);
}
输出:
Constructor was Called!

程序3:

CPP

// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"
using namespace std;
 
// Class A
class A {
    int a;
 
public:
    int* ptr;
 
    // Constructor of class A
    A()
    {
        cout << "Constructor was Called!"
             << endl;
    }
 
    // Destructor of class A
    ~A()
    {
        cout << "Destructor was Called!"
             << endl;
    }
};
 
// Driver Code
int main()
{
 
    // Object Created of class A
    A *a = new A;
    return 0;
}
输出:
Constructor was Called!

即使使用语句“ return 0”,也不会进行Destructor调用。原因在于分配类的对象的不同。当我们创建一个具有class_name object_name的对象时,该对象具有自动存储期限,即,超出范围时将自动销毁该对象。但是,当我们使用新的class_name时,该对象具有动态存储期限,这意味着必须使用delete关键字显式删除该对象。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”