-2

I'm trying to delete a node on a linked list but after the delete command I tried to display the data inside the node and I could still display the supposedly deleted data. Search is the node im trying to delete

int position=0;
  while(admintemp !=NULL)
    {
        position=position+1;
        if(admintemp==search)
        {
            cout<<"found"<<position;
            getch();
            break;

        }   
        admintemp = admintemp->next;    
    }


    node *body = new node;
    node *admintemp = new node;
    if(position>0)
    {
        admintemp = adminhead;

        for (int i= 1;i<position;i++)
        {
            body = admintemp;
            admintemp = admintemp->next;
        }
        body->next=admintemp->next;
        cout<<"deleting";
        getch();
        delete admintemp;
    }    
Han Kenny
  • 5
  • 4
  • 2
    The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read [**How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)**] At least leave us with a **[MCVE]** that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 16 '16 at 20:42
  • @πάνταῥεῖ The program actually executed. Debugger and compiler wasn't able to find any errors. After deleting the node, I tried to traverse the list and the program crashed. – Han Kenny Aug 16 '16 at 20:51
  • 1
    Learn how to use the debugger actually! – πάντα ῥεῖ Aug 16 '16 at 20:57
  • Thanks.Solved. I tried another approach to solve this. – Han Kenny Aug 16 '16 at 21:01
  • 1
    @HanKenny That the program is _syntactically correct_ doesn't mean it's going to work correctly. Look at a sentence like _"I'm always telling the truth, and I'll never be lying to you."_ Are you able to think of ways to break that statement miserably? – πάντα ῥεῖ Aug 16 '16 at 21:03
  • Problem was because I was deleting head node during trial. I added a few lines to delete head node if ever the program was supposed to do so. works fine i guess.Thanks – Han Kenny Aug 16 '16 at 21:05
  • You may want to read these: http://stackoverflow.com/a/367662/5910058 , http://en.cppreference.com/w/cpp/language/ub , http://blog.regehr.org/archives/213 , https://en.m.wikipedia.org/wiki/Undefined_behavior , http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html – Jesper Juhl Aug 16 '16 at 21:11

1 Answers1

2

Reading data via a deleted pointer is "Undefined Behaviour" - meaning that the compiler can do what it pleases and your program has no meaning.

You may get the results you expect, you may get a crash, you may get demons flying out your nose. There is no way to tell. The program is simply invalid and any behaviour is OK. As per the rules of the C++ standard it is the programmers responsibility to not invoke undefined behaviour - it may compile and run, but you've broken the rules so the compiler has no obligation to do anything sane (or even anything at all).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • When I try to traverse the list after the deletion. It display some data on the node then crash. Is this because of the "undefined behavior"? – Han Kenny Aug 16 '16 at 20:47
  • That is allowed. As is not doing anything. As is rebooting your PC. "Undefined Behaviour" means "You broke the rules by doing what you did and the compiler can now do *whatever it wants*" - it literally has *no obligations*. The way to fix it is to "*stop doing that*". – Jesper Juhl Aug 16 '16 at 20:51
  • Problem was because I was deleting head node during trial. I added a few lines to delete head node if ever the program was supposed to do so. works fine i guess.Thanks – Han Kenny Aug 16 '16 at 21:11