C++ 中的 new、malloc() 和 free() 、delete
我们在 C++ 中使用 new
和 delete
运算符来动态分配内存,而 malloc()
和 free()
函数在 C 和 C++ 中也用于相同目的。 new
或 malloc()
和 delete
或 free()
的功能似乎相同,但它们在不同方面有所不同。
构造函数和析构函数调用的行为在以下方面有所不同:
malloc() 与 new():
malloc(): 它是一个 C 库函数,也可以在 C++ 中使用,而 new
运算符仅适用于 C++。
malloc()
和 new
都用于在堆中动态分配内存。但是 new
确实调用了类的构造函数,而 malloc()
没有。
下面是说明 new
和 malloc()
功能的程序:
#include "bits/stdc++.h"\nusing namespace std;
// Class A
class A {
int a;
public:
int* ptr;
// Constructor of class A
A()
{
cout << "Constructor was Called!"\n << endl;
}
};
// Driver Code
int main()
{
A* a = new A;
cout << "Object of class A was "\n << "created using new operator!"\n << endl;
A* b = (A*)malloc(sizeof(A));
cout << "Object of class A was "\n << "created using malloc()!"\n << endl;
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()和delete:
free()
是一个 C 库函数,也可以在 C++ 中使用,而 delete
是 C++ 关键字。
free()
释放内存但不调用类的析构函数,而 delete
释放内存并调用类的析构函数。
下面是说明 new
和 malloc()
函数的程序:
#include "bits/stdc++.h"\nusing namespace std;
// Class A
class A {
int a;
public:
int* ptr;
// Constructor of class A
A()
{
cout << "Constructor was Called!"\n << endl;
}
// Destructor of class A
~A()
{
cout << "Destructor was Called!"\n << endl;
}
};
// Driver Code
int main()
{
// Create an object of class A
// using new operator
A* a = new A;
cout << "Object of class A was "\n << "created using new operator!"\n << endl;
delete (a);
cout << "Object of class A was "\n << "deleted using delete keyword!"\n << endl;
cout << endl;
A* b = (A*)malloc(sizeof(A));
cout << "Object of class A was "\n << "created using malloc()!"\n << endl;
free(b);
cout << "Object of class A was "\n << "deleted using free()!"\n << 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()!
以下是更多的程序示例:
程序A:
// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"\nusing namespace std;
// Class A
class A {
int a;
public:
int* ptr;
// Constructor of class A
A()
{
cout << "Constructor was Called!"\n << endl;
}
// Destructor of class A
~A()
{
cout << "Destructor was Called!"\n << endl;
}
};
// Driver Code
int main()
{
// Object Created of class A
A a;
return 0;
}
运行结果:
Constructor was Called!
Destructor was Called!
在上面的程序中,即使没有使用删除操作符,析构函数仍然被调用。析构函数调用的原因是语句 return 0
。该语句在主函数中执行时会调用为其创建对象的每个类的析构函数。
为了避免析构函数调用,可以将语句 return 0
替换为 exit(0)
。下面是相同的代码:
程序代码示例二:
// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"\nusing namespace std;
// Class A
class A {
int a;
public:
int* ptr;
// Constructor of class A
A()
{
cout << "Constructor was Called!"\n << endl;
}
// Destructor of class A
~A()
{
cout << "Destructor was Called!"\n << endl;
}
};
// Driver Code
int main()
{
// Object Created of class A
A a;
exit(0);
}
运行结果:
Constructor was Called!
示例代码三:
// C++ program to illustrate new, delete
// malloc() and free()
#include "bits/stdc++.h"\nusing namespace std;
// Class A
class A {
int a;
public:
int* ptr;
// Constructor of class A
A()
{
cout << "Constructor was Called!"\n << endl;
}
// Destructor of class A
~A()
{
cout << "Destructor was Called!"\n << endl;
}
};
// Driver Code
int main()
{
// Object Created of class A
A *a = new A;
return 0;
}
运行结果:
Constructor was Called!
即使使用了 return 0
语句,也没有析构函数调用。原因在于分配类的对象的不同。当我们在一个块中创建一个类名 object_name
的对象时,该对象有一个自动存储持续时间,即它会在超出范围时自动销毁。但是当使用 new class_name
时,该对象具有动态存储持续时间,这意味着必须使用 delete
关键字显式删除它。