0

我有一个调用以下函数的代码

int if_nan(double a)                                                            
{                                                                               
    return a!=a;                                                              
} 

if_nan()在计算中会遇到to find 。当我使用 Valgrind 进行 memcheck 时,出现以下错误:

 ==3484== Conditional jump or move depends on uninitialised value(s)
 ==3484==    at 0x804B0A9: if_nan (sph.c:71)
 ==3484==    by 0x8051B78: pressure_forces (pressure_force.c:21)
...

我不明白这里要初始化什么值。请提出一种避免此错误的方法。谢谢你

4

1 回答 1

1

understand通过发布您的代码的 1/100,我们并不是您想要的!

但是,请检查以下部分以进行澄清。请,从下一次开始,尝试将整个代码放在这里。它是免费的。:-)

情况1

代码

#include <stdio.h>
#include <stdlib.h>

int if_nan(double a)
{
    return a!=a;
}


int main()
{

        double p;
        int result;
        p = 3.5;
        result = if_nan(p);
        printf("\n\nresult is %d\n\n", result);
        return 0;
}

输出/输出

[sourav@localhost ~]$ gcc -g so_test3.c -o test
[sourav@localhost ~]$ valgrind --leak-check=full ./test
==24217== Memcheck, a memory error detector
==24217== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==24217== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==24217== Command: ./test
==24217== 


result is 0

==24217== 
==24217== HEAP SUMMARY:
==24217==     in use at exit: 0 bytes in 0 blocks
==24217==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==24217== 
==24217== All heap blocks were freed -- no leaks are possible
==24217== 
==24217== For counts of detected and suppressed errors, rerun with: -v
==24217== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
[sourav@localhost ~]$

案例2

代码

#include <stdio.h>
#include <stdlib.h>

int if_nan(double a)
{
    return a!=a;
}


int main()
{

        double p;
        int result;
        //p = 3.5;      
        result = if_nan(p);
        printf("\n\nresult is %d\n\n", result);
        return 0;
}

输出/输出

[sourav@localhost ~]$ gcc -g so_test3.c -o test
[sourav@localhost ~]$ valgrind --leak-check=full ./test
==24229== Memcheck, a memory error detector
==24229== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==24229== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==24229== Command: ./test
==24229== 


==24229== Use of uninitialised value of size 4
==24229==    at 0x830C3B: _itoa_word (in /lib/libc-2.5.so)
==24229==    by 0x8343D0: vfprintf (in /lib/libc-2.5.so)
==24229==    by 0x83BE82: printf (in /lib/libc-2.5.so)
==24229==    by 0x80483DF: main (so_test3.c:17)
==24229== 
==24229== Conditional jump or move depends on uninitialised value(s)
==24229==    at 0x830C43: _itoa_word (in /lib/libc-2.5.so)
==24229==    by 0x8343D0: vfprintf (in /lib/libc-2.5.so)
==24229==    by 0x83BE82: printf (in /lib/libc-2.5.so)
==24229==    by 0x80483DF: main (so_test3.c:17)
==24229== 
==24229== Conditional jump or move depends on uninitialised value(s)
==24229==    at 0x8327A0: vfprintf (in /lib/libc-2.5.so)
==24229==    by 0x83BE82: printf (in /lib/libc-2.5.so)
==24229==    by 0x80483DF: main (so_test3.c:17)
==24229== 
result is 0

==24229== 
==24229== HEAP SUMMARY:
==24229==     in use at exit: 0 bytes in 0 blocks
==24229==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==24229== 
==24229== All heap blocks were freed -- no leaks are possible
==24229== 
==24229== For counts of detected and suppressed errors, rerun with: -v
==24229== Use --track-origins=yes to see where uninitialised values come from
==24229== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 12 from 8)
[sourav@localhost ~]$

看起来很熟悉?:-) [别往心里放]

于 2013-12-06T12:43:54.300 回答