c - 通过 C 中的 tokenise 函数从 malloc 泄漏内存

标签 c memory-leaks tokenize

当我从我编写的标记化函数中退出我的程序(使用 valgrind 发现)时,我遇到了内存泄漏。在这个函数之外,在我将标记分配给其他变量之后,我会在适当的地方调用 free(tokens) ,但这并不能解决问题。任何帮助将不胜感激!

代码:

/**
 * Splits user input into an array of tokens.
 **/
char ** tokenize(const char * s, int * n)
{
   /* Sets array of strings and allocates memory, sized appropriately. */
   int i;
   char * token;   
   char ** tokens = malloc((BUF_LEN + EXTRA_SPACES) *sizeof(*token));
   char buf[BUF_LEN];
   strncpy(buf, s, BUF_LEN);

   /* Defines first token by a whitespace. */
   token = strtok(buf, " ");

   i = 0;
   /* While loop defines all consequent tokens also with a whitespace. */
   while (token)
   {
      tokens[i] = malloc((strlen(token)+EXTRA_SPACES) *sizeof(*token));
      strncpy(tokens[i], token, strlen(token));
      i++;
      token = strtok(NULL, " ");
   }
   * n = i;
   return tokens;
}

最佳答案

我添加了一个函数来释放你的数组,并用 valgrind 检查它没有内存泄漏。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <memory.h>
size_t BUF_LEN = 32;
int EXTRA_SPACES = 16;
int length = 0;
char ** tokenize(const char * s, int * n)
{
    /* Sets array of strings and allocates memory, sized appropriately. */
    int i;
    char * token;
    char ** tokens = malloc((BUF_LEN + EXTRA_SPACES) *sizeof(*token));
    char buf[BUF_LEN];
    strncpy(buf, s, BUF_LEN);

    /* Defines first token by a whitespace. */
    token = strtok(buf, " ");

    i = 0;
    /* While loop defines all consequent tokens also with a whitespace. */
    while (token)
    {
        tokens[i] = malloc((strlen(token)+EXTRA_SPACES) *sizeof(*token));
        strncpy(tokens[i], token, strlen(token));
        i++;
        token = strtok(NULL, " ");
        length++;
    }
    * n = i;
    return tokens;
}

/* deallocates an array of arrays of char*, calling free() on each */
void free_argv(char **argv, unsigned rows) {
    for (unsigned row = 0; row < rows; row++) {

        free(argv[row]);
    }
    free(argv);
}

int main ()
{
int i = 12;
    char **  ch = tokenize("abc", &i);
    free_argv(ch, (unsigned) length);

}

输出

 valgrind ./a.out 
==28962== Memcheck, a memory error detector
==28962== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==28962== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==28962== Command: ./a.out
==28962== 
==28962== 
==28962== HEAP SUMMARY:
==28962==     in use at exit: 0 bytes in 0 blocks
==28962==   total heap usage: 2 allocs, 2 frees, 67 bytes allocated
==28962== 
==28962== All heap blocks were freed -- no leaks are possible
==28962== 
==28962== For counts of detected and suppressed errors, rerun with: -v
==28962== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

关于c - 通过 C 中的 tokenise 函数从 malloc 泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37360705/

相关文章:

c - 是否有 C 函数来获取文件数据段的大小?

c - 从零开始的标准 C XCode 项目

java - 在计算器中识别一元减号,java

c++ - 如何使用 Boost Regex 标记化 C++

C++/Boost 在多个字符上拆分字符串

c - 函数 sscanf 不考虑宽度字段

c - SIMD (AVX) 比较

c - setpwent 显示 valgrind 中的内存泄漏

c++ - 为什么指针成员初始化为非零?

iphone - MPMoviePlayerController 释放问题