0
bool StudentList::remove(const char * studentName)
{
    for (int i = 0; i < MAX_SIZE; i++)
    {
        if (this->students[i]->isEqualTo(studentName)) // Finds a name to remove
        {
            cout << "Remove: "; // Displays name wished to be removed
            students[i]->print();
            // delete[] students[i]; - Crashes 
            // students[i] = NULL; - Replaces removed name with null, stops working.  
            // students[i]->~Student(); - Call deconstructor, Crashes.
            return true;
        }
    }
    return false;
}

I just want to remove a single element out of the array, but keeps crashing when i delete that element.

students[i] is a pointer array, and i need to remove selected elements

  • 4
    Possible duplicate of [Remove an array element and shift the remaining ones](http://stackoverflow.com/questions/879603/remove-an-array-element-and-shift-the-remaining-ones) – MSD Feb 24 '17 at 05:17
  • See the solutions offered in [this](http://stackoverflow.com/questions/9246165/how-to-remove-elements-from-dynamically-allocated-array) old SO question. – Jeroen Heier Feb 24 '17 at 05:19
  • We need to see the definition of `StudentList::students` (is it an array, a pointer, a vector of unique_ptrs, or what?), and of the base type). We also need to know what "stops working" means, and *when* the code crashes. This is probably going to get closed if we don't have a [mcve]. – Martin Bonner supports Monica Feb 24 '17 at 08:22
  • Final thought, you need to read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Martin Bonner supports Monica Feb 24 '17 at 08:22

2 Answers2

0

It seems that you want to delete each instance of students, if you could find the studentname.

students seems a two dimensional structure pointer to a pointer. ie; **students. But, you are deleting it in wrong way.As you first need to delete the instance of students[i], then delete the instance of students.

Also, since you are calling the destructor students[i]->~Student(); after deleting instance, it may crash again, as you have assigned student[i] = NULL. then it will be, NULL->~Student() -it will also lead crash.

You need to delete it in following way :

for (int i = 0; i < MAX_SIZE; i++)
{
    if (this->students[i]->isEqualTo(studentName)) // Finds a name to remove
    {
        students[i]->~Student();

        delete students[i];

        students[i] = NULL; 
    }
 }
 delete[] students;

 students = NULL; 
Rishi
  • 1,387
  • 10
  • 14
0

First quetion, if you really need to delete "Student" object. If yes, you can add some bad code like:

students[i] = nullptr;

If your students are stored not only in this array, you can make that storage responsible for their deleting. But both ways aren't very good because of using null pointers later. Learn how to use collections, for example vector. You will be able just remove the pointer from an array.

Nikita Smirnov
  • 813
  • 8
  • 17