I have a function that should delete the elements in a dynamic array of pointers.
I have built a function that dynamically creates this array which seems to be working fine (please let me know if you see any issues).
However, my deletion function will not let me delete the value at student[i], spcifically line 137. The code compiles fine with this line commented out, but the value needs to be deleted or else it will cause a memory leak.
Why can't I call delete student[i] like I am attempting at line 137? What is the issue? I am trying to learn and would appreciate a simplified answer.
Here is my code:
#include <iostream>
#include <cstring>
#include <string> //TESTING
#include <fstream> //needed to use files (contains definitions for ifstream and ofstream)
#include <stdlib.h> //for exit
#include <cctype> //char handling functions
#include <iomanip> //setprecision ,etc
#include <cstddef>
using namespace std;
const int SIZE = 101; //max size of arrays
struct Student
{
char * name;
float gpa;
};
Student ** createStudentList(char ** names, int size);
bool destroyStudentList(Student ** studentList, int size);
int main ()
{
Student ** studentList;
char ** names;
int size = 0;
names = new char*[3];
names[size] = new char[strlen("Lindsay")+1];
strcpy(names[size], "Lindsay");
size++;
names[size] = new char[strlen("Emily") +1 ];
strcpy(names[size], "Emily");
size++;
studentList = createStudentList(names, size);
cout << "//TESTING: before destroyStudentList()" << endl;
destroyStudentList(studentList, size);
return 0;
}
// The function creates an array of pointers to Student objects dynamically.
//It allocates Student objects and set their name as the passed in “names” and gpa as 0. “size” is the number of “names” available.
// You can allocate 2*size as many pointers to Student objects just in case you need to add more to the list.
//For the extra spots in the array, set them to nullptr. The function returns the pointer to the Student pointer array.
Student ** createStudentList(char ** names, int size)
{
// code adapted from CS162 module 8 discussion post for Lab 6 (Nancy Chan and Li Liang)
int double_size = size *2;
Student ** studentList = new Student * [double_size];
Student * studentPtr = new Student [double_size];
for (int i = 0; i < 2 * size; i++)
{
studentList[i] = &studentPtr[i];
}
for (int i = 0; i < size; i++)
{
studentList[i]->name = new char[strlen(names[i]) + 1];
strcpy(studentList[i]->name, names[i]);
studentList[i]->gpa = 0;
}
for (int i = size; i < double_size; i++)
{
studentList[i] = NULL;
}
return studentList;
}
// The function deletes all the Student objects in the array along with their dynamically allocated data members,
// such as name. It also releases the array of the pointers to the Student objects.
// The function returns true if the operation is successful and false if “studentList” contains nullptr.
bool destroyStudentList(Student * studentList[], int size)
{
int double_size = size *2;
// When the pointer is nullptr, there is nothing to delete
if (studentList == nullptr)
{
return false;
}
else
{
//cout << "//TESTING: in destroyStudentList. Else..." << endl;
for (int i = 0; i < double_size; i++)
{
if (studentList[i] != NULL) {
cout << (*studentList[i]).name << endl;
delete [] (*studentList[i]).name;/////can't delete the char array here
cout << "//TESTING1: delete [] studentList[i]->name;" << endl;
(*studentList[i]).name = NULL;
cout << "//TESTING2: studentList[i]->name = NULL;" << endl;
delete studentList[i];////////////WHY DOES THIS CAUSE AN EXCEPTION?
//cout << "//TESTING3: delete studentList[i];" << endl;
//
//studentList[i] = NULL;
//cout << "//TESTING4: studentList[i] = NULL;" << endl;
}
}
}
delete studentList;
studentList = nullptr;
return true;
}
std::vector? Andstd::stringinstead ofchar*?