What code should we put in the space labeled "\\insert code here" to avoid memory leaks, compiler and runtime errors? cl
Posted: Fri Apr 29, 2022 7:14 am
What code should we put in the space labeled "\\insert code here" to avoid memory leaks, compiler and runtime errors? class Student { public: Student (const string& n ="000000000", const string& s ="Hey you!"); public: string number; string name; }; int main(){ vector<Student*>* students = new vector<Student*>; students->push_back(new Student("123", "Alice")); students->push_back(new Student("456","Bob")); students->push_back(new Student("789", "Charlie")); undefined //insert code here return 0; for (int i = 0; i < 3; ++i) { delete (*students); Nothing. When the vector goes out of scope it will delete itself and all data. delete students; for (int i = 0; i < 3; ++i) { delete (*students);
What code should we put in the space labeled "\\insert code here" to avoid memory leaks, compiler and runtime errors? #include <string> #include <vector> using namespace std; class Student { public: Student (const string& n = "000000000",const string& s = "Hey you!"); private: string number; string name; int main(){ vector<Student*> students; undefined students.push_back(new Student("123", "Alice")); students.push_back(new Student("456", "Bob")); students.push_back(new Student("789", "Charlie")); //insert code here return 0;
delete students; for (int i = 0; i < 3; ++i) { delete students; for (int i 0; i < 3; ++i) { delete students; } delete students; Nothing, the vector will delete itself and all data when it goes out of scope.
What code should we put in the space labeled "\\insert code here" to avoid memory leaks, compiler and runtime errors? #include <string> #include <vector> using namespace std; class Student { public: Student (const string& n = "000000000",const string& s = "Hey you!"); private: string number; string name; int main(){ vector<Student*> students; undefined students.push_back(new Student("123", "Alice")); students.push_back(new Student("456", "Bob")); students.push_back(new Student("789", "Charlie")); //insert code here return 0;
delete students; for (int i = 0; i < 3; ++i) { delete students; for (int i 0; i < 3; ++i) { delete students; } delete students; Nothing, the vector will delete itself and all data when it goes out of scope.