简单程序的内存泄漏,如何释放allocs?

时间:2013-03-02 02:15:50

标签: c

我正在学习C,并且在找到如何释放我的malloc()方面遇到了问题。

该程序运行正常..但即时通讯使用valgrind,它提出了8个allocs和5个frees。我需要能够再免费3个。我评论了哪些我认为我没有解放但我不确定解决方案。

有没有办法可以释放这些分配,或者我是否需要考虑重写tokenizer()?

以下是整个文件的代码。

#include <stdlib.h>
#include <stdio.h> 
#include <string.h>

char *substr(const char *s, int from, int nchars) {

   char *result = (char *) malloc((nchars * sizeof(char))+1);
   strncpy(result, s+from, nchars);

   return result;
}

/**
 Extracts white-space separated tokens from s.
 @param s A string containing 0 or more tokens.
 @param ntokens The number of tokens found in s.
 @return A pointer to a list of tokens. The list and tokens must be freed
 by the caller.
 */ 
char **tokenize(const char *s, int *ntokens) {
    int fromIndex = 0;
    int toIndex = 0;
    char **list;
    int finalCount = *ntokens;
    int count = 0;

    list = malloc(*ntokens * sizeof(char*));

    while ( count < finalCount) {

        char *m = strchr(s,' ');
        toIndex = m - s;

        if(toIndex >= 0) {
            list[count] = substr(s,fromIndex,toIndex); // This substr() gets free'ed from main()        
            s = substr(s, toIndex+1, strlen(s));    // I believe This is where I am making extra mallocs that are not being freed
            count++;
        } else {        
            list[count] = substr(s,fromIndex,strlen(s)); // This substr() gets free'ed from main()
            count++;
        }
    }   

    return list;
} 

int main(int argc, char **argv) {
  char **list;
  char *string = "terrific radiant humble pig";

  int count = 4; // Hard-Coded

  list = tokenize(string, &count);

  for (int i=0;i<count;i++) {
    printf("list[%d] = %s\n", i, list[i]);
  }

  // Free mallocs()'s
  for (int i=0;i<count;i++) {
    free(list[i]);
  }
  // Free List
  free(list);

  return 0;
}  

3 个答案:

答案 0 :(得分:2)

获取一个令牌后,您不需要每次都使用子程序。就时间和景观而言,这太浪费了。您只需更改s的值,使其指向您需要的字符串。

//s = substr(s, toIndex+1, strlen(s));    // You don't need have to generate a new string
s = s + toIndex + 1;//You can just change the value of s to make it point to the string you need

答案 1 :(得分:0)

我可以想到一个简单的解决方法,只需在覆盖之前将s的当前值存储在另一个指针中。并且还要确保不要将s的第一个值直接作为tokenize()的参数释放。

char **tokenize(const char *s, int *ntokens) {
    int fromIndex = 0;
    int toIndex = 0;
    char **list;
    int finalCount = *ntokens;
    int count = 0;
    bool firstTime = true; // Use this to make sure you do not free up the memory for the initial s passed as the function arg

    list = malloc(*ntokens * sizeof(char*));

    while ( count < finalCount) {
        char *m = strchr(s,' ');
        toIndex = m - s;

        if(toIndex >= 0) {
            const char* previous_s = s; // Store the current value of s
            list[count] = substr(s,fromIndex,toIndex); // This substr() gets free'ed from main()        
            s = substr(previous_s, toIndex+1, strlen(previous_s));
            if (!firstTime)
            {
                free(previous_s); // Since we're done with the previous_s, we can free up the memory
            }
            firstTime = false;
            count++;
        } else {        
            list[count] = substr(s,fromIndex,strlen(s)); // This substr() gets free'ed from main()
            count++;
        }
    }   

    if (!firstTime)
    {
        free(s); // There could be a block allocated last time which needs to be freed as well
    }

    return list;
}

答案 2 :(得分:0)

问题正是您认为的问题! 幸运的是,在c中很容易移动一个字符串,你不需要再次调用substr;因为指针; - )

// s = substr(s, toIndex+1, strlen(s));   
s += toIndex+1;