c代码中奇怪的valgrind错误
我的h/w在c中是这样的:c代码中奇怪的valgrind错误,c,valgrind,C,Valgrind,我的h/w在c中是这样的: #include <stdio.h> #include <stdlib.h> int main() { unsigned long int a,b,m, answer; int i, k; scanf("%ld", &a); scanf("%ld", &b); scanf("%ld", &m); k = b >> 63; if (k & 1)
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long int a,b,m, answer;
int i, k;
scanf("%ld", &a);
scanf("%ld", &b);
scanf("%ld", &m);
k = b >> 63;
if (k & 1)
answer = a * 2;
for(i=1;i<64;i++)
{
k = b >> (63-i);
if (k & 1)
answer = (((answer%m * 2)%m) + (a)%m)%m;
else
answer = (((answer%m * 2)%m)%m)%m;
}
printf("%ld\n", answer);
return 0;
}
此外,它仍然存在此错误。然后,在程序变量答案的开头用零初始化的所有变量都可以取消初始化,如果scanf失败,初始化ita、b和m可能会取消初始化,因此答案和k的初始化可能取决于这些值,就像后续的if语句一样。并将%ld更改为%lu。
==6557== Memcheck, a memory error detector
==6557== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6557== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6557== Command: /home/solution
==6557== Parent PID: 6556
==6557==
==6557== Use of uninitialised value of size 8
==6557== at 0x537044B: _itoa_word (in /lib64/libc-2.15.so)
==6557== by 0x5372B90: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x5370455: _itoa_word (in /lib64/libc-2.15.so)
==6557== by 0x5372B90: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x5372BEE: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x53727A3: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x537281D: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557==
==6557== HEAP SUMMARY:
==6557== in use at exit: 0 bytes in 0 blocks
==6557== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6557==
==6557== All heap blocks were freed — no leaks are possible
==6557==
==6557== For counts of detected and suppressed errors, rerun with: -v
==6557== Use —track-origins=yes to see where uninitialised values come from
==6557== ERROR SUMMARY: 7 errors from 5 contexts (suppressed: 2 from 2)