4

I'm writing code for a lab in class, which is an exercise in OOD design using a circular linked list. This just means that a few key functions that are used aren't accessible to me. However, I'm mostly confused because although my driver mimics the one written by the professor, I'm still getting the mchk error in the title. Here is the code it references

{
int nNodesFreed{0};
node* n{head};

for(; n!= head || ! nNodesFreed; n = n->next) {
    delete n;
    nNodesFreed++;
    }
cout << "# nodes freed: " << nNodesFreed << endl;
}

I saw in a similar question that the problem could be that I'm trying to access memory that has already been freed. I.E. how can n = n->next if n doesn't exist anymore. I tried switching to a while loop using a current and a next pointer, but that made the problem even worse. The code works perfectly in my professor's version of the assignment, in which I haven't implemented the functions that I need to.

The exact error I'm given is:

Invalid read of size 8 
  at 0x400D8A: main (lab04.cpp:28) // this references the for loop
Address 0x5a02048 is 8 bytes inside a block of size 16 free'd
  at 0x4C28FAC: operator delete(void*) 
  by 0x400D81: main (lab04.cpp:29)

Thanks for any help

FutureShocked
  • 779
  • 1
  • 10
  • 26
  • 2
    `delete n` then `n = n->next`??? – barak manos Dec 05 '14 at 20:16
  • And yet, it totally works in the version of the assignment without my functions implemented – FutureShocked Dec 05 '14 at 20:18
  • 1
    Undefined behavior - it can work and it can fail. – barak manos Dec 05 '14 at 20:18
  • @FutureShocked That's the problem with code with bugs -- it doesn't do what you expect. Fix the bugs and the mysteries will go away. – David Schwartz Dec 05 '14 at 20:39
  • @DavidSchwartz The problem is that the code runs fine. The only issue is the one specified in the OP, and I don't know how to fix that. – FutureShocked Dec 05 '14 at 20:47
  • 1
    @FutureShocked It's very simple -- don't access an object after you `delete` it. Your code does that, that's the issue. That can cause unpredictable behavior. – David Schwartz Dec 05 '14 at 20:48
  • @DavidSchwartz Unfortunately, changing the code so that it doesn't do that actually creates more issues of the same nature – FutureShocked Dec 05 '14 at 20:57
  • @FutureShocked Sorry to hear that. But you probably should fix your bugs regardless, it's the only way to get software that's reliable and trustworthy. It is a pain, I agree. Programmers spend an enormous amount of time fixing their own mistakes. – David Schwartz Dec 05 '14 at 21:03

1 Answers1

5

You are accessing n after it has been deleted. That is causing undefined behavior.

Further, you're not checking for n->next to be valid: you deleted head in the first iteration. Does deleting n cause head to be updated? If not, then you'll reach undefined behavior again when you reach the end of the linked list (which may be caused either by deleteing a nullptr or else deleteing a garbage pointer (depending on what n->next on the end of the linked list points to).

inetknght
  • 4,300
  • 1
  • 26
  • 52