Unhandled exception throw: write access violation.
item was nullptr.
I don't know why the item is a garbage value when debugging.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct _list_node
{
struct _list_node *link;
int coefficient;
int order;
} } NODE;
static NODE *polyAdd(NODE *head1, NODE *head2)
{
NODE *item = NULL;
NODE *p1 = head1;
NODE *p2 = head2;
NODE *head, *tail = NULL;
while (1)
{
if (p1 != NULL && p2 != NULL)
{
if (p1->order == p2->order)
{
item->coefficient = p1->coefficient + p2->coefficient; /* Error*/ where
item->order = p1->order;
p1 = p1->link;
p2 = p2->link;
}
else
{
if (p1->order > p2->order)
{
item->coefficient = p1->coefficient;
item->order = p1->order;
p1 = p1->link;
}
if (p1->order < p2->order)
{
item->coefficient = p2->coefficient;
item->order = p2->order;
p2 = p2->link;
}
}
}
else
{
if (p1 == NULL)
{
item->coefficient = p2->coefficient;
item->order = p2->order;
p1 = p1->link;
}
if (p2 == NULL)
{
item->coefficient = p1->coefficient;
item->order = p1->order;
p2 = p2->link;
}
}
if (p1 == NULL && p2 == NULL)
{
tail->link = NULL;
break;
}
if (tail = NULL)
{
head = item;
}
else
{
tail->link = item;
}
tail = item;
}
return head;
}
static NODE *create(int *list, int size)
{
int i;
NODE *item, *head = (NODE *)0;
for (i = 0; i < size; i++) {
if (list[i]) {
item = (NODE *)malloc(sizeof(NODE));
if (!item) {
fprintf(stderr, "\nOut of memory.\n");
exit(1);
}
item->coefficient = list[i];
item->order = i;
item->link = head;
head = item;
}
}
return(head);
}
static int getSize(int *list)
{
int size = 0;
if (!list)
return(0);
while (list[size] >= 0)
size++;
return(size);
}
static void printList(NODE *head)
{
NODE *item = head;
while (item) {
if (item != head)
printf(" +");
if (item->order)
printf(" %d e%d", item->coefficient, item->order);
else
printf(" %d", item->coefficient);
item = item->link;
}
printf("\n");
}
static void deleteList(NODE **head)
{
NODE *del, *item;
if (!head)
{
fprintf(stderr, "[deleteList] usage of function is wrong.\n");
exit(1);
}
item = *head;
while (item)
{
del = item;
item = item->link;
free(del);
}
*head = (NODE *)0;
}
void main(void)
{
int list1[] = { 1, 2, 3, 4, 5, 6, -1 };
int list2[] = { 6, 5, 4, 3, 2, 1, -1 };
int list3[] = { 2, 7, 0, 3, 0, 8, -1 };
int list4[] = { 0, 0, 8, 0, 2, 9, 8, -1 };
int n1, n2;
NODE *root, *head1, *head2;
/*
* * Easy
*/
n1 = getSize(list1);
n2 = getSize(list2);
head1 = create(list1, n1);
head2 = create(list2, n2);
root = polyAdd(head1, head2);
printf("\nPolynomial 1 :"); printList(head1);
printf("Polynomial 2 :"); printList(head2);
printf("Added result :"); printList(root);
deleteList(&head1);
deleteList(&head2);
deleteList(&root);
/*
* * Hard case 1
*/
n1 = getSize(list3);
n2 = getSize(list4);
head1 = create(list3, n1);
head2 = create(list4, n2);
root = polyAdd(head1, head2);
printf("\nPolynomial 1 :"); printList(head1);
printf("Polynomial 2 :"); printList(head2);
printf("Added result :"); printList(root);
deleteList(&root);
/*
* * Hard case 2
*/
root = polyAdd(head2, head1);
printf("\nPolynomial 1 :"); printList(head2);
printf("Polynomial 2 :"); printList(head1);
printf("Added result :"); printList(root);
deleteList(&head1);
deleteList(&head2);
deleteList(&root);
}
The error is caused by item - > coefficient in NULL initialized state (NODE * item = NULL;).
static NODE * polyAdd(NODE * head1, NODE * head2) {
NODE * item = NULL;
NODE * p1 = head1;
NODE * p2 = head2;
NODE * head, * tail = NULL;
while (1) {
if (p1 != NULL && p2 != NULL) {
if (p1 - > order == p2 - > order) {
item ->coefficient = p1 - >coefficient + p2 - >coefficient; /* Error*/ where
item - > order = p1 - > order;
p1 = p1 - > link;
p2 = p2 - > link;
} } else {
if (p1 - > order > p2 - > order) {
item - > coefficient = p1 - > coefficient;
item - > order = p1 - > order;
p1 = p1 - > link;
}
if (p1 - > order < p2 - > order) {
item - > coefficient = p2 - > coefficient;
item - > order = p2 - > order;
p2 = p2 - > link;
}
}
} } else {
if (p1 == NULL) {
item - > coefficient = p2 - > coefficient;
item - > order = p2 - > order;
p1 = p1 - > link;
}
if (p2 == NULL) {
item - > coefficient = p1 - > coefficient;
item - > order = p1 - > order;
p2 = p2 - > link;
}
}
if (p1 == NULL && p2 == NULL) {
tail - > link = NULL;
break;
}
if (tail = NULL) {
head = item;
} } else {
tail - > link = item;
}
tail = item;
}
return head;
}
© 2024 OneMinuteCode. All rights reserved.