簡體   English   中英

如何釋放單鏈接列表中的內存

[英]How do I free up memory in Singly Linked List

關於如何釋放在FreeList函數中分配給char * (在CreateList函數中分配)的內存的任何建議?

基本上,我會將CreateList的根作為函數參數返回給FreeList函數。

我嘗試使用

temp = head;
head = head->next;
free(temp->str);        
free(temp);     

但它也失敗了。

LIST *CreateList(FILE *fp) 
{
    /* Variable declaration */
    char input[BUFF];
    LIST *root = NULL;
    size_t strSize;    
    LIST *newList;            

    /* Read till end of file */
    while (fscanf(fp, "%255s", input) != EOF) 
    {
        strSize = strlen(input) + 1;

        /* Function to determine if we shud create a new node or increment node count */
        if (!ListSame(root, input))  
        {
            /* New node */
            if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) 
            {
                printf("Out of memory...");
                exit(EXIT_FAILURE);
            } 
           if ((newList->str = (char *)malloc(sizeof(strSize))) == NULL) 
            {
               printf("Not enough memory for %s", input);
               exit(EXIT_FAILURE);
            }
            memcpy(newList->str, input, strSize);
            newList->count = 1;
           //determine if it is root
           if (root == NULL)
           {
               newList->next = NULL;
               root = newList;
               }
               else
               {
                   newList->next = root->next;
                   root->next = newList;
               }
           }
        }
    return root;
}


  void FreeList(LIST *head)
  {
      LIST *temp = NULL;
      char* str;
      /* loop from root till end */
      while (head != NULL)
      {  
          temp = head;
          str = temp->str;
          head = head->next;
          free(str);
          free(temp);         
      }
 }
void FreeList(LIST *head)
{
    LIST *temp = NULL;
    /* loop from root till end */
    while (head != NULL)
    {  
       temp = head;
       head = head->next;     
       free(temp->str); /* Free the string does not matter if str is null */   
       free(temp);
       /* How do i free the dynamic allocated memory for char * */         
    }
}

我認為您應該尋找。

您可以使用與現有代碼相同的原理:

char* str = head->str;
if(str != NULL)
{
    free(str);
}

我認為這值得嘗試(因為將指針傳遞給另一個函數,所以傳遞指針的地址):

調用函數:FreeList(&head)

調用函數:void FreeList(LIST ** head)

您的列表是遞歸定義的類型...為什么不使用use遞歸?

void FreeList(LIST *node)
{
    if (node == null) /* end of the line */
        return;
    LIST *next = node->next; /* save a copy of the next to be freed */
    /* free the contents of the current node */
    free(node->str);
    free(node);
    FreeList(next); /* free the next */
}

您沒有為字符串正確分配內存:

if ((newList->str = (char *)malloc(sizeof(strSize))) == NULL)

size_t類型分配足夠的內存-而不是字符串所需的字符數。

它應該是:

if ((newList->str = (char *)malloc(strSize)) == NULL)

實際上,更好的是:

if ((newList->str = strdup(input)) == NULL)

那么您也可以擺脫執行memcpy()的代碼行。

使用strdup()來處理常見的字符串復制在C有助於防止愚蠢的錯誤這樣的-這常有事。

暫無
暫無

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

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