当我退出我的程序(使用ValgReND发现)时,我遇到了一个内存泄漏。在这个函数之外,在我将令牌分配给其他变量之后,我在适当的地方调用free(令牌),但这并不能解决问题。任何帮助都将不胜感激!
代码

/**
 * 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;
}

最佳答案

我在free数组中添加了一个函数,并用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/

10-17 01:27