C语言 Valgrind 在 C 中无效的 free()/delete/delete[]/realloc()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36815937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Valgrind Invalid free() / delete / delete[] / realloc() in C
提问by Luke
Valgrind tells me, that there is a leak in a memory, I've tried to free() it but I suppose that it's not done properly. Any ideas? Thank You.
Valgrind 告诉我,内存中存在泄漏,我曾尝试 free() 它,但我认为它没有正确完成。有任何想法吗?谢谢你。
Invalid free() / delete / delete[] / realloc() at 0x4C27D4E: free (vg_replace_malloc.c:427)
by 0x400C00: main (main.c:149)
Address 0x51ba138 is 0 bytes after a block of size 8 alloc'd
at 0x4C28BED: malloc (vg_replace_malloc.c:263) by 0x400B0E: main (main.c:119)
HEAP SUMMARY: in use at exit: 2 bytes in 1 blocks total heap usage: 5 allocs, 5 frees, 14 bytes allocated
2 bytes in 1 blocks are definitely lost in loss record 1 of 1
at 0x4C28BED: malloc (vg_replace_malloc.c:263)
by 0x40084F: strdup (main.c:19)
by 0x4009C4: permute (main.c:83)
by 0x400B9C: main (main.c:138)
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] = ' ..
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
}
'; //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
}
回答by Stian Skjelstad
Before I read your code. Valgrind reporting invalid free error is not because of leaked memory, but you try to free invalid memory (or free same memory more than once). Compiling your code with the compilter flag -g (gdb debug), will enable more debug information, making the backtraces from valgrind much better.
在我阅读你的代码之前。Valgrind 报告 invalid free 错误不是因为内存泄漏,而是您尝试释放无效内存(或多次释放相同内存)。使用编译器标志 -g (gdb debug) 编译您的代码,将启用更多调试信息,从而使 valgrind 的回溯更好。
A quick guess:
快速猜测:
double free or corruption (out): 0x000000000074a060 ***
回答by David C. Rankin
Among other issues, a primary issue with your code is:
除其他问题外,您的代码的主要问题是:
char **permuts, **work;
...
work = permuts;
...
free (work); //free the memory //line 149
free (permuts); //free the memory
caused by:
造成的:
##代码##You are freeing the same block of memory twice.
您正在两次释放同一块内存。

