There is a code:

#include <iostream> #include <limits> #include <conio.h> using namespace std; struct stack { int inf; stack *head, *next; }; void addElements(stack *&stack1); void showStack(stack *stack1); void clearStack(stack *stack1); int menu(); void input(int &a); void deleteMaxValue(stack *stack1); int main() { stack *stack1 = new stack; stack1->head = NULL; while (true) { switch (menu()) { case 1: cout << "Enter the element:" << endl; addElements(stack1); cout << "Element added" << endl; break; case 2: showStack(stack1); break; case 3: clearStack(stack1); break; case 4: deleteMaxValue(stack1); break; case 0: cout << "Press Enter if you want to exit" << endl; if (_getch() == 13) { delete stack1->head; delete stack1; return 0; } break; default: cout << "Choose 1-4 or 0" << endl; break; } } } void addElements(stack *&stack1) { int inf; input(inf); stack *temp = new stack; temp->inf = inf; temp->next = stack1->head; stack1->head = temp; } void showStack(stack *stack1) { cout << "--------------Stack--------------" << endl; stack *temp = stack1->head; while (temp != NULL) { cout << "\t\t"<<temp->inf << endl; temp = temp->next; } cout << endl; } void clearStack(stack *stack1) { while (stack1->head != NULL) { stack *temp = stack1->head->next; delete stack1->head; stack1->head = temp; } cout << "Stack cleared" << endl; } int menu() { cout << "1 - add element" << endl; cout << "2 - show stack" << endl; cout << "3 - clear stack" << endl; cout << "4 - delete max element" << endl; cout << "0 - exit" << endl; int choise; input(choise); return choise; } void input(int &a) { while (true) { cin >> a; if (cin.good()) { break; } cout << "Wrong input" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } void deleteMaxValue(stack *stack1) { stack *temp = stack1->head; int max = temp->inf; while (temp != NULL) { if (temp->inf > max) { max = temp->inf; } temp = temp->next; } stack *p = stack1; temp = p->next; while (temp != NULL) { if (temp->inf == max) { p->next = temp->next; delete temp; temp = p->next; } else { p = temp; temp = temp->next; } } stack1 = temp; } 

The task is to remove the maximum stack item. The deletion is performed in the void deleteMaxValue(stack *stack1) function void deleteMaxValue(stack *stack1) , but when executed it throws the following exception: An exception was thrown:

read access violation. temp was 0xCDCDCDCD

. in the if (temp->inf == max) line if (temp->inf == max) . What is the mistake and how to get rid of it?

    1 answer 1

    In deleteMaxValue(stack *stack1) there is the line stack *p = stack1; , although above it in a similar value stack *temp = stack1->head;

    In addition, the stack1 variable stack1 passed by value, unlike the other changing function addElements(stack *&stack1) .

    And it seems that this is not all problems; for the sake of some sorting out dependencies, I would suggest that the "controlling" element stack1 be a separate structure of another type. It has semantic differences: for example, it does not contain an integral value and only it can exist and represent an “empty stack” when there are no values ​​yet.

    And now you can even accidentally apply the function of managing the stack to an element that does not manage the stack, and nothing good will come of it.