Valgrind 在 C 中无效的 free()/delete/delete[]/realloc()

Luk*_*uke 2 c malloc free valgrind memory-leaks

Valgrind 告诉我,内存中存在泄漏,我曾尝试 free() 它,但我认为它没有正确完成。有任何想法吗?谢谢你。

0x4C27D4E 处的 free()/delete/delete[]/realloc() 无效:free (vg_replace_malloc.c:427)

by 0x400C00: main (main.c:149)

地址 0x51ba138 是大小为 8 的块分配后的 0 字节

在 0x4C28BED: malloc (vg_replace_malloc.c:263) by 0x400B0E: main (main.c:119)

堆摘要:退出时使用:1 个块中的 2 个字节总堆使用:5 个分配,5 个释放,14 个分配的字节

1 个块中的 2 个字节在丢失记录 1 of 1 中肯定丢失

在 0x4C28BED:malloc (vg_replace_malloc.c:263)

由 0x40084F: strdup (main.c:19)

通过 0x4009C4:置换(main.c:83)

由 0x400B9C: main (main.c:138)

char *strdup (const char *s)
{
    char *d = malloc (strlen (s) + 1);  // Space for length plus null //line 19
    if (d == NULL) {
        return NULL;            // No memory
    }
    strcpy (d, s);              // Copy the characters
    return d;                   // Return the new string
}

void permute (char *arrayOfPermutations, int startIndex, int stopIndex,
            char ***permuts)
{
    int i;
    if (startIndex == stopIndex) {
        **permuts = strdup (arrayOfPermutations);       //save generated string //line 83
        *permuts += 1;          //increment location
    } else {
        for (i = startIndex; i <= stopIndex; i++) {
            swap ((arrayOfPermutations + startIndex),
                (arrayOfPermutations + i));
            permute (arrayOfPermutations, startIndex + 1, stopIndex, permuts);
            swap ((arrayOfPermutations + startIndex),
                (arrayOfPermutations + i));
        }
    }
}

int main (int argc, char *argv[])
{
    char *stringInput, c = 0;
    unsigned int j = 0, i = 0, stringSize, facto;
    char **permuts, **work;

    stringInput = (char *) malloc (sizeof (char));

    while (c != '\n') {
        c = getc (stdin);       //read the input from keyboard standard input
        stringInput = (char *) realloc (stringInput, (j + 1) * sizeof (char));  //re-allocate (resize) memory for character read to be stored
        stringInput[j] = c;     //store read character by making pointer point to c
        j++;
    }

    stringInput[j - 1] = '\0';  //add null termination
    stringSize = strlen (stringInput);
    facto = factorial (stringSize);

    permuts = (char **) malloc (facto * sizeof (char *));       // allocate n! pointers //line 119
    work = permuts;

    printf ("String size: %d\n", stringSize);

    ...some printfs here...permute (stringInput, 0, stringSize - 1, &work);     //creates permutations of chars //line 138

    qsort (permuts, facto, sizeof (char *), compare);   //sorts strings alphabetically

    for (i = 0; i <= facto - 1; i++) {
        printf ("\"%s\"\n", permuts[i]);
    }

    free (work);                //free the memory //line 149
    free (permuts);             //free the memory
    free (stringInput);         //free the memory
}
Run Code Online (Sandbox Code Playgroud)

Sti*_*tad 6

在我阅读你的代码之前。Valgrind 报告 invalid free 错误不是因为内存泄漏,而是您尝试释放无效内存(或多次释放相同内存)。使用编译器标志 -g (gdb debug) 编译您的代码,将启用更多调试信息,从而使 valgrind 的回溯更好。

快速猜测:

  ..
  for(i = 0; i <= facto-1; i++)
   {
    printf("\"%s\"\n", permuts[i]);
    free (permuts[i]); /* free the text, allocated with strdup(); */
  }

  free(permuts);//free the memory
  free(stringInput);//free the memory
}
Run Code Online (Sandbox Code Playgroud)