Refer to GPT and my own idea, this error is usually caused by the operation of the freed memory, it is recommended to check whether the code has access to the freed memory.
Depending on your description, you may need to check whether there is a problem with the adjustment process of the linked list structure that could cause the linked list structure to be broken. You can also check to see if memory is allocated and released correctly, if there are any memory leaks, etc.
Here are some problems and solutions that may cause access rights conflicts:
Accessing the memory that has been freed
If you try to access the memory region after the memory has been freed, you will result in an access permission conflict. To prevent this from happening, you can either set the pointer to NULL after freeing memory, or check to see if the pointer is NULL before freeing memory.
Memory out of bounds
Access outside the valid range of an array or pointer to a memory region results in an access permission conflict. To avoid this, check the valid range of an array or pointer before accessing it, or use a more secure data structure such as std::vector, etc.
Multiple threads accessing the same memory region
In multithreaded programming, if multiple threads access the same memory region at the same time, access permission conflicts may occur. To avoid this, you can use thread synchronization mechanisms such as locking.
Incorrect memory allocation and release
Incorrect operations during memory allocation and release may result in access permission conflicts. To prevent this from happening, you can carefully review the code that allocates and frees memory to ensure that it is correct. You can also use tools, such as valgrind, to check for memory allocation and release problems.
Based on the information you provided, the code needs to be modified as follows:
Dynamically allocates and frees memory for linked list nodes rather than creating nodes on the stack. This avoids data loss due to the small stack space.
Before freeing node memory, we need to set the next member of the node pointer to NULL. This avoids data loss due to hanging Pointers.
During the sorting process, it is necessary to determine if the list header pointer needs to be updated. If the list header pointer changes, the header pointer needs to be updated after the sorting is complete.
Before printing the result, you need to determine whether the linked list is empty. If the list is empty, a corresponding prompt should be printed.
Finally, you should avoid using Chinese characters and non-ASCII characters in if statements and printf statements to avoid coding problems.
Here is the modified code:
void Analysis() {
system("cls");
good *tail, *q, *p;
tail = q = p = NULL;
int count = 0, i, j;
p = head;
while (p != NULL) {
count++;
p = p->next;
}
good *a = (good*)malloc(sizeof(good));
a->next = NULL;
tail = a;
p = head;
for (i = 0; i < count - 1; i++) {
j = count - i - 1;
tail = a;
tail->next = head;
q = tail->next;
p = q->next;
while (j--) {
if (q->profit < p->profit) {
q->next = p->next;
p->next = q;
tail->next = p;
}
tail = tail->next;
q = tail->next;
if (q->next == NULL)
break;
else
p = q->next;
}
}
head = a->next;
free(a);
if (head == NULL) {
printf("No product information entered!\n");
} else {
printf("The most profitable product is: %s\n", head->name);
printf("The profit is: %.2f\n", head->profit);
printf("It is recommended to increase sales of this product!\n");
}
system("pause");
}
Note that this is just a modification to the code you provided, and there may still be other problems. It is recommended that you carefully observe the output of the program running during testing so that problems can be detected and resolved promptly.