C 为什么我的程序会泄漏12字节的内存?未初始化的值来自哪里?
我在我的程序上运行了valgrind(正如预期的那样),但valgrind报告了12字节的内存泄漏和一些我在代码中找不到的未初始化值实例 该程序是一个简单的二次方程求解器,绝大多数数学内容都在quadSolver.c中进行,而输入验证和以干净、直观的方式将结果返回给用户则在main.c中 主要条款c:C 为什么我的程序会泄漏12字节的内存?未初始化的值来自哪里?,c,memory,memory-leaks,valgrind,C,Memory,Memory Leaks,Valgrind,我在我的程序上运行了valgrind(正如预期的那样),但valgrind报告了12字节的内存泄漏和一些我在代码中找不到的未初始化值实例 该程序是一个简单的二次方程求解器,绝大多数数学内容都在quadSolver.c中进行,而输入验证和以干净、直观的方式将结果返回给用户则在main.c中 主要条款c: #include <stdlib.h> #include "quadSolver.h" int main(int argv, char* args[]){ i
#include <stdlib.h>
#include "quadSolver.h"
int main(int argv, char* args[]){
if (argv != 4) {
printf("Invalid arguments.\n");
printf("Input: ./solve x y z\n");
exit(0);
} else if (!isNumber(args[1]) || !isNumber(args[2]) || !isNumber(args[3])) {
printf("Invalid arguments.\n");
printf("Arguments must be integers.\n");
exit(0);
}
int* a = calloc(1, sizeof(int));
int* b = calloc(1, sizeof(int));
int* c = calloc(1, sizeof(int));
*a = strtol(args[1], NULL, 10);
*b = strtol(args[2], NULL, 10);
*c = strtol(args[3], NULL, 10);
double** roots = calloc(3, sizeof(int));
roots = findRoots(*a, *b, *c);
if(*roots[0] == 0) {
printf("Roots are imaginary.\n");
} else if(*roots[0] == 1) {
printf("Roots are real and equal.\n");
printf("Roots = %f\n", *roots[1]);
} else {
printf("Roots are real and unequal.\n");
printf("Root A = %f\n", *roots[1]);
printf("Root B = %f\n", *roots[2]);
}
free(a);
free(b);
free(c);
free(roots);
}
瓦尔格林说:
==34332== Conditional jump or move depends on uninitialised value(s)
==34332== at 0x1093E1: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332== Uninitialised value was created by a stack allocation
==34332== at 0x1093CA: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332==
==34332== Conditional jump or move depends on uninitialised value(s)
==34332== at 0x1093EB: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332== Uninitialised value was created by a stack allocation
==34332== at 0x1093CA: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332== HEAP SUMMARY:
==34332== in use at exit: 12 bytes in 1 blocks
==34332== total heap usage: 6 allocs, 5 frees, 1,056 bytes allocated
==34332==
==34332== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==34332== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==34332== by 0x1093AA: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332==
==34332== LEAK SUMMARY:
==34332== definitely lost: 12 bytes in 1 blocks
==34332== indirectly lost: 0 bytes in 0 blocks
==34332== possibly lost: 0 bytes in 0 blocks
==34332== still reachable: 0 bytes in 0 blocks
==34332== suppressed: 0 bytes in 0 blocks
==34332==
您正在泄漏main()中的内存:
double**root=calloc(3,sizeof(int))代码>
下一行对findRoots()
的调用在其内部分配内存,并在返回时覆盖root
的值,最后泄漏3*sizeof(int)
(在main内部分配)字节,由于前面提到的覆盖,该字节现在无法访问
然而,正如评论中提到的,您的程序充满了其他类型的bug,但是这个答案的范围是解决内存泄漏问题
编辑:为了便于将来参考,在调试时,您应该使用调试信息(例如gcc/clang-g…)编译程序因此,valgrind有更多的上下文可供使用,并且当您可以(例如)直接在输出中获取行号时,更容易找出泄漏。strtol
返回long
,但您为int
分配了内存。还有double**root=calloc(3,sizeof(int))代码>不可能是对的。@WeatherVane哦,是的,哦,接得好。我会解决这些问题,看看它是否有助于解决漏洞。@据我所知,WeatherVane long可以隐式转换为int,在修复calloc以分配3个sizeof(long)块后,我仍然有相同的错误。long
在某些系统上是最小32位和64位,因此,如果您想使用long
,则不应随意将其与int
或long
混合使用。您的“3块”不需要是3*sizeof(double*)
?但是使用calloc
时,最好根据要分配的变量传递大小,因此double**roots=calloc(3,sizeof(*roots))代码>@WeatherVane谢谢你教我这个。不幸的是,这个问题似乎与未初始化的值和内存泄漏无关。
==34332== Conditional jump or move depends on uninitialised value(s)
==34332== at 0x1093E1: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332== Uninitialised value was created by a stack allocation
==34332== at 0x1093CA: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332==
==34332== Conditional jump or move depends on uninitialised value(s)
==34332== at 0x1093EB: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332== Uninitialised value was created by a stack allocation
==34332== at 0x1093CA: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332== HEAP SUMMARY:
==34332== in use at exit: 12 bytes in 1 blocks
==34332== total heap usage: 6 allocs, 5 frees, 1,056 bytes allocated
==34332==
==34332== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==34332== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==34332== by 0x1093AA: main (in /home/lugehr/Documents/Programming Practice/C/QuadraticSolver/solve)
==34332==
==34332== LEAK SUMMARY:
==34332== definitely lost: 12 bytes in 1 blocks
==34332== indirectly lost: 0 bytes in 0 blocks
==34332== possibly lost: 0 bytes in 0 blocks
==34332== still reachable: 0 bytes in 0 blocks
==34332== suppressed: 0 bytes in 0 blocks
==34332==