📜  在C ++中删除对象数组

📅  最后修改于: 2021-05-31 22:51:35             🧑  作者: Mango

需要删除对象

  • 为了避免内存泄漏,因为当使用new动态创建对象时,该对象将占用堆部分中的内存。
  • 如果未明确删除对象,则程序将在运行时崩溃。

程序1:创建使用new运算符动态创建的类的对象,并使用delete运算符其显式删除

C++
// C++ program to create an object
// dynamically and  delete explicitly
#include 
using namespace std;
  
// Class
class Student {
  
public:
    // Constructor
    Student()
    {
        cout << "Constructor is called!\n";
    }
  
    // Destructor
    ~Student()
    {
        cout << "Destructor is called!\n";
    }
  
    // Function to display the message
    void write()
    {
        cout << "Writing!\n";
    }
};
  
// Driver Code
int main()
{
    // Create an array of objects
    Student* student = new Student();
  
    // Function Call to write()
    // using instance
    student->write();
  
    // De-allocate the memory
    // explicitly
    delete student;
  
    return 0;
}


C++
// C++ program to create an array of
// objects and deleting it explicitly
#include 
using namespace std;
  
// Class
class Student {
  
public:
    // Constructor
    Student()
    {
        cout << "Constructor is called!\n";
    }
  
    // Destructor
    ~Student()
    {
        cout << "Destructor is called!\n";
    }
  
    // Function to display message
    void write()
    {
        cout << "Writing!\n";
    }
};
  
// Driver Code
int main()
{
    // Create an array of the object
    // dynamically
    Student* student = new Student[3];
  
    // Function Call to write()
    student[0].write();
    student[1].write();
    student[2].write();
  
    // De-allocate the memory
    // explicitly
    delete[] student;
  
    return 0;
}


C++
// C++ program to delete array of
// objects
#include 
using namespace std;
  
// Class
class Student {
  
public:
    // Constructor
    Student()
    {
        cout << "Constructor is called!\n";
    }
  
    // Destructor
    ~Student()
    {
        cout << "Destructor is called!\n";
    }
  
    // Function to display message
    void write()
    {
        cout << "Writing!\n";
    }
};
  
// Driver Code
int main()
{
    // Create an object dynamically
    Student* student = new Student[3];
  
    // Function call to write()
    student[0].write();
    student[1].write();
    student[2].write();
  
    // De-allocate the memory
    // explicitly
    delete student;
  
    return 0;
}


输出:
Constructor is called!
Writing!
Destructor is called!

程序2:使用new运算符动态创建对象数组。每当在运行时创建类的对象数组时,程序员都有责任删除它并避免内存泄漏:

C++

// C++ program to create an array of
// objects and deleting it explicitly
#include 
using namespace std;
  
// Class
class Student {
  
public:
    // Constructor
    Student()
    {
        cout << "Constructor is called!\n";
    }
  
    // Destructor
    ~Student()
    {
        cout << "Destructor is called!\n";
    }
  
    // Function to display message
    void write()
    {
        cout << "Writing!\n";
    }
};
  
// Driver Code
int main()
{
    // Create an array of the object
    // dynamically
    Student* student = new Student[3];
  
    // Function Call to write()
    student[0].write();
    student[1].write();
    student[2].write();
  
    // De-allocate the memory
    // explicitly
    delete[] student;
  
    return 0;
}
输出:
Constructor is called!
Constructor is called!
Constructor is called!
Writing!
Writing!
Writing!
Destructor is called!
Destructor is called!
Destructor is called!

程序3:

下面是其中删除用于删除对象的数组的程序:

C++

// C++ program to delete array of
// objects
#include 
using namespace std;
  
// Class
class Student {
  
public:
    // Constructor
    Student()
    {
        cout << "Constructor is called!\n";
    }
  
    // Destructor
    ~Student()
    {
        cout << "Destructor is called!\n";
    }
  
    // Function to display message
    void write()
    {
        cout << "Writing!\n";
    }
};
  
// Driver Code
int main()
{
    // Create an object dynamically
    Student* student = new Student[3];
  
    // Function call to write()
    student[0].write();
    student[1].write();
    student[2].write();
  
    // De-allocate the memory
    // explicitly
    delete student;
  
    return 0;
}

说明:该程序将在运行时崩溃。在此程序中,构造函数正常工作,但析构函数仅对第一个对象执行,并且在该程序由于内存泄漏而在运行时崩溃后,才对它进行处理。这是因为在运行时创建了3个对象,但仅删除了一个对象,这就是为什么其余两个对象在运行时崩溃的原因。

结论
在C++中,使用以下命令删除在运行时使用运算符创建的类的单个对象: delete运算符,而使用delete []运算符删除对象数组时,它不会导致内存泄漏。

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