클래스 용 데이터를 처리하고이를 별도의 목록으로 정렬하는 프로그램을 작성합니다.C : 이중 링크드 목록의 첫 번째 항목을 삭제할 때 Segfault
지금까지 데이터를 보관할 목록을 만든 다음 각 "고객"과 "구입 한 도서"별로 목록을 배열했습니다.
목록에서 첫 번째 항목을 삭제하려고 할 때 segfault가 표시됩니다.
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 기능이있다 :
가 여기에 deleteFirst 기능의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.
:
// 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의 일부 :
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)
는 다음 주에 내 코드입니다 .
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;
}
아이디어가 있으십니까? 감사.
==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)
편집 : 는 그리고이 Valgrind의 보여줍니다 무엇을 나는 목록에 남아있는 단 하나의 요소가있을 때 첫 번째 요소를 삭제하려고 할 때 문제가 발생 믿습니다. 만약 적어도 2 개 요소를 포함하는리스트의 첫 번째 요소를 삭제할 때
정확히 어디에서 충돌이 발생합니까? 또한 이것은 모든 코드가 아닙니다. 사용자가 표시하지 않은 기능 중 하나에 문제가있을 수 있습니다. http://www.sscce.org/ –
을 참조하십시오 ListHndl이 포인터입니까? 어떻게 insertOrder에 전달됩니까? 적어도 2 -3 개의 삽입이 올바르게 수행 된 인쇄 기능을 작성하여 확인할 수 있습니까? 질문/코드는 매우 추상적이어서 문제가 위의 코드에 속하는지 아닌지 추측하기 어렵습니다. – fayyazkl
@fayyazkl, 죄송합니다. 코드가 업데이트되었습니다. – user3288944