-1

我是 C 的新手,我似乎在实现链表时遇到了很多问题。List 有效,无效的是内存管理。我不是最擅长的,但我似乎做错了一些事情。

我从 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

我发现你的代码有两个问题...

问题1:销毁函数应该是这样的,

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);
}

中调用销毁函数后的代码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 回答