也许答案是显而易见的,或者Valgrind可能是错的,但我有内存泄漏,我能够隔离这段代码,这些代码给出了同样的错误:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *values = (int*)malloc(3 * sizeof(int));
for (int i = 0; i < 3; i++)
{
values[i] = i;
}
printf("values: %d %d %d\n", values[0], values[1], values[2]);
return 0;
}
Valgrind报告的错误是:
==2354== Conditional jump or move depends on uninitialised value(s)
==2354== at 0x1003FCC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==2354== by 0x1001F0BB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==2354== by 0x1001FB005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==2354== by 0x1002209CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==2354== by 0x100220CA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==2354== by 0x1001F6B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==2354== by 0x1001F49F7: printf (in /usr/lib/system/libsystem_c.dylib)
==2354== by 0x100000F45: main (example.c:12)
==2354== Uninitialised value was created by a stack allocation
==2354== at 0x1001F6C18: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
我无法弄清楚印刷品是如何印刷未经初始化的价值的。我错过了什么?