There is a part of the code:

struct Spis2 { int info; Spis2 *next, *prev; }; void Del_5(Spis2 *&begin, Spis2 *&end) { Spis2 *temp = begin; int counter = 0; while (temp != NULL) { counter++; temp = temp->next; } temp = begin; while (temp != NULL) { if (temp->info % 5 == 0 && temp->info % 2 != 0) { //If the first if (temp->prev == NULL) { if (counter == 1) { begin = end = NULL; } else { temp->next->prev = NULL; begin = temp->next; } delete temp; } //if the last else if (temp->next == NULL) { temp->prev->next = NULL; end = temp->prev; delete temp; } //If the middle else { temp->prev->next = temp->next; temp->next->prev = temp->prev; delete temp; } } else { temp = temp->next; } } } 

The task is to remove items ending in 5 from the bidirectional list. However, an error occurs during execution: An exception was thrown: read access violation.

temp was 0x8123.

And points to the string

 if (temp->info % 5 == 0 && temp->info % 2 != 0) { 

What is the error and how to solve?

  • 2
    delete temp; and at the next iteration temp->info - VTT
  • if (temp-> info% 5 == 0 && temp-> info% 2! = 0) can be replaced by if (temp-> info% 10 == 5), and VTT indicated the cause of the error - AR Hovsepyan
  • you, after deleting an element ( delete temp ), must somehow move on to the next element. And you do not go to it and continue to work with an already deleted item. - vegorov

0