0

I have:

class Node {
   int* vec;
   Node* next;
};

Class LinkedList{
   Node* head;
}

I made a function that found the node that I want to delete:

Node* tmp = find("abc");

I ordered the pointers and I debug it and everything is OK.

now I have to delete the tmp, so I tried:

delete[] tmp->vec;
delete tmp; // here I get an error window.

why?

THIS IS MY REAL CODE:

class Show_Time
{
  private:
    string movieCode;
    string movieName;
    string time; //the time of screening the movie.
};


class Time_LinkedList
{
 private:
    class Time_Node
    {
     public:
        Show_Time* data;
        Time_Node* prev;
        Time_Node* next;
     public:
        Time_Node(string movie_code, string movie_name, string time_of_movie); //"Time_Node" constructor
        ~Time_Node();//"Time_Node" destructor
    };
    Time_Node* head; //pointer to the first node in the linkedlist
};


void Time_LinkedList::delete_TimeNode(string movieCode, string time)
{
    Time_Node* tmp = find_Time_Node(movieCode,time);

    // the case that there is one element in the list
    if (tmp->prev == NULL && tmp->next == NULL)
    {
        head = NULL;
    }

    // the case that it's the first element of the list
    else if (tmp->prev == NULL)
    {
        head = tmp->next;
        tmp->next->prev = head;        
    }

    // the case that it's the last element of the list
    else if (tmp->next == NULL)
    {
        tmp->prev->next = NULL;
    }

    // there are element in the left and right of the element
    else
    {
        tmp->prev->next = tmp->next;
        tmp->next->prev = tmp->prev;
    }

    // delete the temp and its data
    delete tmp->data;
    delete tmp;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138

1 Answers1

1

So according to your response your issue is that you are doing a double delete which is undefined behavior. You should remove the delete data from Time_LinkedList::delete_TimeNode and let the destructor do it's job.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740