简体   繁体   中英

Got memory leak; how to free this malloc?

bool check(const char *word)
{
    int length = strlen(word);

    //malloc size of char times length of word plus \0
    char *lower_case = malloc(sizeof(char) * (length + 1));
    lower_case[length + 1] = '\0';

    //change characters to lowercase
    for (int i = 0; i < length; i++)
    {
        lower_case[i] = tolower(word[i]);
    }

    //generate int hash
    int index = generate_hash(lower_case);

    node_ptr trav = hashtable[index];

    while (trav != NULL)
    {
        if (strcmp(trav->word, lower_case) == 0)
        {
            return true;
        }
        trav = trav -> next;
    }
    free(lower_case);

    return false;
}

I got 27 bytes of memory leaked from a Valgrind test; how to free it?

  • lower_case[length + 1] = '\0'; writes out of bounds, change to [length] .
  • You are missing #include <stdlib.h> and other necessary includes.
  • You create a memory leak each time you execute return true; .
  • Never hide pointers behind typedefs, as taught by crap classes like CS-50.

You should be able to fix the code along the lines of this:

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>

bool check (const char *word) 
{ 
  size_t length = strlen(word);

  char* lower_case = malloc(length + 1);
  if(lower_case == NULL)
  {
    return false;
  }

  //change characters to lowercase
  for (size_t i = 0; i < length; i++)
  {
    lower_case[i] = tolower(word[i]);
  }
  lower_case[length] = '\0'; 

  //generate int hash
  int index = generate_hash(lower_case); 
  bool result = false;
  for(const node* trav = hashtable[index]; trav!=NULL; trav=trav->next)
  {
    if (strcmp(trav->word, lower_case) == 0)
    {
        result = true;
        break;
    }
  }

  free(lower_case);
  return result;
}

The type node_ptr needs to be changed into node without hidden pointers.

There's an out of bounds access immediately after malloc .

Here, you're accessing out of bounds:

lower_case[length + 1] = '\0';

It should be:

lower_case[length] = '\0';

It's also a sensible idea to check if malloc failed, too!

As noted in comments, there's also a case when memory leak can happen when returning from inside the loop. You need to free there:

  if (strcmp(trav->word, lower_case) == 0)
    {
        free(lower_case);
        return true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM