我有一个函数,应该删除动态指针数组中的元素。

我建立了一个可以动态创建此数组的函数,该函数似乎运行良好(请告诉我您是否遇到任何问题)。

但是,我的删除功能不允许我删除student[i]处的值,尤其是第137行。代码在此行注释掉的情况下可以正常编译,但是该值需要删除,否则会导致内存泄漏。

为什么不能像在第137行中一样尝试呼叫delete student[i]?有什么问题我正在尝试学习,希望能得到一个简化的答案。

这是我的代码:

#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;

}

最佳答案

你问:


delete studentList[i];////////////WHY DOES THIS CAUSE AN EXCEPTION?



在我回答那条线为什么不好的问题之前,让我从一个简单的例子开始。
如果您通过以下方式分配内存:

int* ip = new int[10];


释放内存的唯一有效方法是使用:

delete [] ip;


以下是尝试重新分配该内存的所有无效方法。

delete ip;
delete [] ip+2;
delete [] ip+4;
delete ip+2;
delete ip+4;
delete &ip[2];
delete &ip[4];


线

delete studentList[i];


遭受那个问题。您不仅没有使用delete运算符的数组形式,而且还在使用一次调用分配的数组中的各个元素上调用delete

createStudentList中,使用以下命令为Student的数组分配内存:

 Student * studentPtr = new Student [double_size];


然后使用以下命令将这些指针分配给studentList

   for (int i = 0; i < 2 * size; i++)
   {
      studentList[i] = &studentPtr[i];
   }


删除Student数组的唯一有效方法是使用:

delete [] studentList[0];

关于c++ - 为什么不能删除此数组元素的指针值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28802210/

10-15 00:16
查看更多