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

📅  最后修改于: 2021-09-14 02:20:17             🧑  作者: Mango

我们在 C++ 中使用 new 和 delete运算符来动态分配内存,而 malloc() 和 free() 函数在 C 和 C++ 中也用于相同的目的。 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()!

在上面的程序中我们可以清楚地看到,在使用new运算符创建对象时调用了默认构造函数,而使用 malloc函数没有调用默认构造函数。
free() 与删除:

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

下面是说明 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()!

以下是更多插图的程序:
方案一:

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)”。下面是相同的代码:
方案二:

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!

方案三:

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”之后也没有析构函数调用。原因在于分配一个类的对象不同。当我们在一个块中创建一个带有class_name object_name的对象时,该对象有一个自动存储期,即它会在超出范围时自动销毁。但是当我们使用new class_name 时,该对象具有动态存储期限,这意味着必须使用delete关键字显式删除它。

想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程