簡體   English   中英

C:刪除雙重鏈接列表中的第一個項目時出現段錯誤

[英]C: Segfault when deleting first item in Doubly Linked List

對於班級,我要編寫一個程序來處理數據並將其排序到單獨的列表中。

到目前為止,我創建了一個列表來保存數據,然后為每個單獨的“客戶”及其“購買的書”創建一個列表數組。

嘗試從列表中刪除第一項時出現段錯誤。

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)

這是我的主要代碼:

//  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)

編輯:我相信當列表中只剩下一個元素時,嘗試刪除第一個元素時就會出現問題。

刪除包含至少2個元素的列表的第一個元素時,將在else分支中刪除列表的第一個元素:

L->first->next->prev = NULL;

之后,您在指令中引用NULL ed指針

L->first = L->first->next;

相反,你應該寫

L->first = tmp->next;

注意:此分析假定在您的鏈表實現中, x->next->prev == x

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM