-1

私はCが初めてで、リンクリストの実装に問題があるようです。リストは機能しますが、機能しないのはメモリ管理です。私はそれが得意ではありませんが、いくつか間違ったことをしているようです。

valgrind からサイズ 8 の無効な読み取りを受け取りました。正確なエラーは次のとおりです。

==2155== Invalid read of size 8
==2155==    at 0x401271: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==  Address 0x8788218 is 8 bytes inside a block of size 24 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
==2155==    by 0x400EB3: main (TestList.c:87)
==2155==
==2155== Invalid read of size 8
==2155==    at 0x401242: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==  Address 0x8788218 is 8 bytes inside a block of size 24 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
==2155==    by 0x400EB3: main (TestList.c:87)
==2155==
==2155== Invalid read of size 8
==2155==    at 0x40124E: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==  Address 0x8788210 is 0 bytes inside a block of size 24 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
==2155==    by 0x400EB3: main (TestList.c:87)
==2155==
==2155== Invalid free() / delete / delete[] / realloc()
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401258: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==  Address 0x8788160 is 0 bytes inside a block of size 104 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x400EE0: main (TestList.c:89)
==2155==
==2155== Invalid free() / delete / delete[] / realloc()
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401264: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==  Address 0x8788210 is 0 bytes inside a block of size 24 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401202: RemoveFromBackOfLinkedList (in /home/btm7984/hw3/TestLis t)
==2155==    by 0x400EB3: main (TestList.c:87)
==2155==
==2155== Invalid free() / delete / delete[] / realloc()
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x400F1C: main (TestList.c:94)
==2155==  Address 0x8787dd0 is 0 bytes inside a block of size 24 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401264: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==
==2155== Invalid free() / delete / delete[] / realloc()
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x400F2B: main (TestList.c:95)
==2155==  Address 0x8787dd0 is 0 bytes inside a block of size 24 free'd
==2155==    at 0x4C2A82E: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-lin ux.so)
==2155==    by 0x401264: DestroyLinkedList (in /home/btm7984/hw3/TestList)
==2155==    by 0x400EFE: main (TestList.c:92)
==2155==
==2155==
==2155== HEAP SUMMARY:
==2155==     in use at exit: 24 bytes in 1 blocks
==2155==   total heap usage: 413,183 allocs, 413,188 frees, 26,444,136 bytes allo cated
==2155==
==2155== LEAK SUMMARY:
==2155==    definitely lost: 24 bytes in 1 blocks
==2155==    indirectly lost: 0 bytes in 0 blocks
==2155==      possibly lost: 0 bytes in 0 blocks
==2155==    still reachable: 0 bytes in 0 blocks
==2155==         suppressed: 0 bytes in 0 blocks
==2155== Rerun with --leak-check=full to see details of leaked memory
==2155==
==2155== For counts of detected and suppressed errors, rerun with: -v
==2155== ERROR SUMMARY: 13 errors from 7 contexts (suppressed: 2 from 2)

これは、次のコードから来ています。

   FILE *fp;
   char tmpString[100];
   LinkedLists *ListPtr = malloc(sizeof(LinkedLists));
   ElementStructs *DataPtr;
   LinkedListNodes* curr = malloc(sizeof(LinkedListNodes));
   int counter = 0;
   int Done = 0;
   if (argc ==2){
   InitLinkedList(ListPtr);
   fp = fopen(argv[1], "r");
   if (!fp){
     fprintf(stderr,"%s Cannot open file %s\n", argv[0], argv[1]);
     exit(1);
   }
   do{
     fscanf(fp,"%s",tmpString);
     if (!feof(fp)) {
       DataPtr = malloc(sizeof(ElementStructs));
       strcpy(DataPtr->str,tmpString);
       DataPtr->index=counter;
       AddToBackOfLinkedList(ListPtr, DataPtr);
       counter++;
       Done = 1;
     } else {
       Done = 0;
     }
   }while (Done);

   printf("The first 6 elements of the LinkedList:\n");
   curr = ListPtr->FrontPtr;
   printf("Element %d is %s\n",curr->ElementPtr->index,curr->ElementPtr->str);
   curr = curr->Next;
   printf("Element %d is %s\n",curr->ElementPtr->index,curr->ElementPtr->str);
   curr = curr->Next;
   printf("Element %d is %s\n",curr->ElementPtr->index,curr->ElementPtr->str);
   curr = curr->Next;
   printf("Element %d is %s\n",curr->ElementPtr->index,curr->ElementPtr->str);
   curr = curr->Next;
   printf("Element %d is %s\n",curr->ElementPtr->index,curr->ElementPtr->str);
   curr = curr->Next;
   printf("Element %d is %s\n\n",curr->ElementPtr->index,curr->ElementPtr->str);


   fclose(fp);
   DestroyLinkedList(ListPtr);
   free(ListPtr);
   free(curr);
   free(curr);
   exit(0);
   return 0;
   } else {
     fprintf(stderr,"Usage: %s <inputfile>", argv[0]);
     return 2;
   }

別のモジュールでは、これらの関数のいくつかを特に DestroyLinkedLists として定義しています。

void DestroyLinkedList(LinkedLists *ListPtr){
   LinkedListNodes* temp = ListPtr->FrontPtr;
   LinkedListNodes* nextTemp = ListPtr->FrontPtr;
   for (;temp->Next!=NULL;){
     nextTemp = temp->Next;
     free (temp->ElementPtr);
     free (temp);
     temp = nextTemp;
   }ListPtr->NumElements=0;
   ListPtr->FrontPtr=NULL;
   ListPtr->BackPtr=NULL;
}

これらのメモリエラーを理解するための助けをいただければ幸いです。

4

1 に答える 1

1

あなたのコードには2つの問題があります...

問題 1: Destroy 関数は次のようにする必要があります。

void DestroyLinkedList(LinkedLists *ListPtr)
{
        while(ListPtr->FrontPtr != NULL)
        {
                LinkedListNodes *removedNode = ListPtr->FrontPtr;
                ListPtr->FrontPtr = ListPtr->FrontPtr->Next;

                /* Deallocate element in node */
                free(removedNode->ElementPtr);

                /* Deallocate node */
                free(removedNode);
        }
        free(ListPtr);
}

で Destroy 関数を呼び出した後のコードmain

...
fclose(fp);
DestroyLinkedList(ListPtr);
//free(ListPtr); //remove these 3 lines
//free(curr);
//free(curr);
...

問題 2: なぜmalloc curr、トラバース目的で使用しているのですか?

ElementStructs *DataPtr;
//LinkedListNodes* curr = malloc(sizeof(LinkedListNodes)); //remove malloc this line and add following line,
LinkedListNodes* curr;
int counter = 0;

ElementStructs..の構造を貼り付けていただけますか?

リンクのように役立つでしょう:)

C リンク リストでのメモリ リーク

于 2012-12-29T04:26:51.887 に答える