对于类I,我要编写一个程序来处理数据并将其分类到单独的列表中。
到目前为止,我创建了一个列表来保存数据,然后创建一个列表,用于每个单独的“客户”及其“购买的图书”。
我在尝试删除列表中的第一项时遇到了段错误。
Please enter the name of your input file.
input1.txt
Reading in: 'input1.txt'
5 10 1 407 2 8127 1 8131 4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779
Just created 5 lists.
Customer ID: 1 BookID: 407
2 8127 1 8131 4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779
Customer ID: 2 BookID: 8127
1 8131 4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779
Customer ID: 1 BookID: 8131
4 22048 5 407 3 64 5 22026 2 19 1 406 3 162779
Customer ID: 4 BookID: 22048
5 407 3 64 5 22026 2 19 1 406 3 162779
Customer ID: 5 BookID: 407
3 64 5 22026 2 19 1 406 3 162779
Customer ID: 3 BookID: 64
5 22026 2 19 1 406 3 162779
Customer ID: 5 BookID: 22026
2 19 1 406 3 162779
Customer ID: 2 BookID: 19
1 406 3 162779
Customer ID: 1 BookID: 406
3 162779
Customer ID: 3 BookID: 162779
List is empty.
Segmentation fault (core dumped)
这是我在main中的代码:
// Create x amount of array with x being the first number in 'holder'.
ListHndl list[getFirst(holder)];
int i;
for (i = 1; i <= getFirst(holder); i++) {
list[i] = newList();
}
printf("Just created %d lists.\n", getFirst(holder));
deleteFirst(holder); // Delete the # of customers
deleteFirst(holder); // Delete the # of purchases
// Iterate and process 'holder'.
long customer, bookID;
while (!isEmpty(holder)) {
customer = getFirst(holder);
deleteFirst(holder);
bookID = getFirst(holder);
deleteFirst(holder);
printf("Customer ID: %d\t BookID: %d\n",customer, bookID);
// insertOrder(list[customer], bookID);
printList(NULL, holder);
}
这是list.h的一部分:
typedef struct NodeStruct {
long data;
struct NodeStruct* next;
struct NodeStruct* prev;
} NodeStruct;
// Rename NodeStruct* as NodePtr
typedef NodeStruct* NodePtr;
typedef struct ListStruct {
NodePtr first;
NodePtr last;
NodePtr current;
} ListStruct;
// ListHndl is just a ListStruct* renamed.
这是deleteFirst函数:
void deleteFirst(ListHndl L) {
assert (!isEmpty(L));
NodePtr tmp = L->first;
if(L->first->next == NULL)
L->last = NULL;
else L->first->next->prev = NULL;
L->first = L->first->next;
free(tmp);
}
这是insertSort函数。
void insertOrder(ListHndl L, long data) {
NodePtr tmp = newNode();
tmp->data = data;
NodePtr prev = NULL;
NodePtr curr = L->first;
while (curr != NULL && data > curr->data) {
prev = curr;
curr = curr->next;
}
if (curr == NULL) L->last = tmp;
if (prev == NULL) L->first = tmp;
else prev->next = tmp;
tmp->next = curr;
}
有什么想法吗?谢谢。 这就是valgrind所展示的:
==498== Invalid read of size 8
==498== at 0x400D41: getFirst (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x400AA1: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==498==
==498==
==498== Process terminating with default action of signal 11 (SIGSEGV)
==498== Access not within mapped region at address 0x0
==498== at 0x400D41: getFirst (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x400AA1: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== If you believe this happened as a result of a stack
==498== overflow in your program's main thread (unlikely but
==498== possible), you can try to increase the size of the
==498== main thread stack using the --main-stacksize= flag.
==498== The main thread stack size used in this run was 10485760.
==498==
==498== HEAP SUMMARY:
==498== in use at exit: 952 bytes in 17 blocks
==498== total heap usage: 39 allocs, 22 frees, 1,480 bytes allocated
==498==
==498== 24 bytes in 1 blocks are still reachable in loss record 1 of 4
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==498== by 0x400B81: newList (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x400958: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498==
==498== 120 bytes in 5 blocks are still reachable in loss record 2 of 4
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==498== by 0x400B81: newList (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x400A34: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498==
==498== 240 bytes in 10 blocks are still reachable in loss record 3 of 4
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==498== by 0x400C0C: newNode (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x40111C: insertOrder (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x400B02: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498==
==498== 568 bytes in 1 blocks are still reachable in loss record 4 of 4
==498== at 0x4A06A2E: malloc (in /opt/rh/devtoolset-2/root/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==498== by 0x35CF8671CA: __fopen_internal (iofopen.c:76)
==498== by 0x4008E8: openFile (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498== by 0x40094A: main (in /afs/cats.ucsc.edu/users/m/rho5/private/cmps101/program2/store)
==498==
==498== LEAK SUMMARY:
==498== definitely lost: 0 bytes in 0 blocks
==498== indirectly lost: 0 bytes in 0 blocks
==498== possibly lost: 0 bytes in 0 blocks
==498== still reachable: 952 bytes in 17 blocks
==498== suppressed: 0 bytes in 0 blocks
==498==
==498== For counts of detected and suppressed errors, rerun with: -v
==498== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
make: *** [valgrind] Segmentation fault (core dumped)
编辑:我认为当列表中只剩下一个元素时,尝试删除第一个元素时会出现问题。
答案 0 :(得分:0)
删除包含至少2个元素的列表的第一个元素时,删除else分支中列表的第一个元素:
L->first->next->prev = NULL;
此后,您使用指令
引用NULL
ed指针
L->first = L->first->next;
相反,你应该写
L->first = tmp->next;
请注意:
此分析假定在您的链接列表实现中x->next->prev == x
。