未初始化的值 - if_nan函数

时间:2013-12-03 15:17:18

标签: c initialization conditional valgrind memcheck

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

int if_nan(double a)                                                            
{                                                                               
    return a!=a;                                                              
} 
在计算中遇到

以查找if_nan()。当我用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)
...

我不明白这里要初始化的价值。请建议一种避免此错误的方法。谢谢

1 个答案:

答案 0 :(得分:1)

我们不会通过发布1/100代码来understand你想要的东西!!!

如何,请查看以下部分以获得澄清。请从下次开始尝试将整个代码放在这里。它免费。 : - )

案例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;
}

O / P

[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;
}

O / P

[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 ~]$

看起来很熟悉? :-) [没有难过的感觉]