1

我正在学习 C,并且在找出如何释放 malloc() 时遇到问题。

该程序运行正确..但我正在使用 valgrind,它提出了 8 个分配和 5 个释放。我需要能够再释放 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;
}  
4

3 回答 3

2

每次获得一个令牌后,您都不需要 substr 。就时间和空间而言,这太浪费了。您可以更改 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
于 2013-03-02T03:28:21.830 回答
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;
}
于 2013-03-02T02:25:59.967 回答
0

问题出在你想的地方!幸运的是,在 c 中移动点非常容易,在哪个字符串处,您不需要再次调用substr;因为指针;-)

// s = substr(s, toIndex+1, strlen(s));   
s += toIndex+1;
于 2013-03-02T03:29:18.173 回答