-1
void deleteBasedOnID(student* head,student* tail,int stu_ID)
{

    if (head == NULL)
    {
        std::cout << "nothing to be deleted";
    }

    else if (stu_ID == head->stu_ID)
    {
        //delete from the beginning
        temp = head->next;
        if (temp == NULL)
        {
            tail = NULL;
        }
        else
        {
            temp->prev = NULL;
        }
        std::cout << "Deleted ID: " << head->stu_ID << std::endl;
        delete head;
        head = temp;
    }

    else
    {
        //start from second 
        temp = head;
        previous = NULL;
        while (stu_ID != temp->stu_ID)
        {
            previous = temp;
            temp = temp->next;
            if (temp == NULL)
            {
                std::cout << "no such ID!" << std::endl;
                return;
            }
        }

        previous->next = temp->next;
        if (temp->next == NULL) 
        {
            tail = previous;
        }
        else
        {
            temp->next->prev = previous;
        }

        std::cout << "Deleted ID: " << temp->stu_ID << std::endl;
        delete temp;

    }

}

I have a student struct and global pointers head and tail, I put the said head and tail into deleteBasedOnID head and tail arguments

it works fine for if(head == NULL) or the else block but however whenever I try to delete from the beginning, it works fine internally but after I tried to display the list, error occurred.

This is my display function body

void display()
{   

    temp = head;
    while (temp != NULL)
    {
        std::cout << "Student ID: " << temp->stu_ID << std::endl;
        temp = temp->next;
    }
}

main function

int main()
{
    head = NULL;
    tail = NULL;
    temp = NULL;

    int id;
    std::cout << std::endl;
    std::cout << "Enter the id you want to delete: ";
    std::cin >> id;
    deleteBasedOnID(head, tail, id);
    std::cout << "New sorted list" << std::endl;
    display();

    return 0;
}
Newbie
  • 549
  • 1
  • 4
  • 9
  • 3
    [0xDDDDDDDD : Used by Microsoft's C++ debugging heap to mark freed heap memory](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – Evg Apr 21 '20 at 11:35
  • You pass `student* head` - a pointer passed by value. So, `head = temp;` or `head = NULL;` adjusts the local `head` but not the global or outer you passed in. A fix (without knowing the rest of your code) is probably to pass `student *&head` instead, i.e. the pointer by reference. So, the original head pointer is managed in your function and adjusted correctly in case. – Scheff's Cat Apr 21 '20 at 11:38
  • 3
    It's never too soon to stop using global variables. – molbdnilo Apr 21 '20 at 11:54
  • I see , thanks Scheff I have learned new thing , much appreciated – Newbie Apr 21 '20 at 12:07
  • Molbdnilo could you explain why ? – Newbie Apr 21 '20 at 12:07
  • ***Molbdnilo could you explain why ?*** They are considered bad practice. Here is a good reason why: [https://stackoverflow.com/a/485020/487892](https://stackoverflow.com/a/485020/487892) – drescherjm Apr 21 '20 at 12:10
  • 1
    @Newbie You're a newbie, so its hard to appreciate when the longest program you've ever written is 30 lines of code. But in medium sized programs (say 1000+ lines) unrestricted use of global variables makes the code impossible to understand. See here for more discussion, https://stackoverflow.com/questions/484635/are-global-variables-bad – john Apr 21 '20 at 12:11

1 Answers1

2

In your deleteBasedOnId-function, you change the value for the parameter head, which is a copy of the pointer to head you made in main. The original pointer in main keeps its value, which now points to deleted memory, hence the segfault. You can take a reference to the pointer to head as parameter to solve this problem:

void deleteBasedOnID(student*& head,student*& tail,int stu_ID)
melk
  • 530
  • 3
  • 9
  • So the delete affects not only the copy of the pointer but also the original pointer ? – Newbie Apr 21 '20 at 12:10
  • But when I assign head = temp only the copy is affected? – Newbie Apr 21 '20 at 12:12
  • The delete does not affect the pointer at all. The delete affects the object that the pointer points to. – Eljay Apr 21 '20 at 12:15
  • Ok I think I kind of get it , this means when I delete head, the object that the head point to was deleted and hence the global head is now pointed to object which has been deleted? – Newbie Apr 21 '20 at 12:21
  • @Newbie yes, that's exactly it. – melk Apr 21 '20 at 12:22