c - 未初始化的值 - if_nan 函数

标签 c initialization conditional-statements 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/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;
}

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

看起来很眼熟吗? :-) [没有难受的感觉]

关于c - 未初始化的值 - if_nan 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354871/

相关文章:

c++ - 如何在 C/C++ 中释放数组

c - 如何使用 C 将字符数组中的两个字符替换为另一个字符?

c++ - 在输出流中使用 `?:`

c - 期望此无符号指针类型转换的输出为正,但结果为负

c++ - 有没有办法删除 C 宏中的引号?

Python:在基类init中调用重写的基类方法

java - 如何从 Condition Autowiring 属性 bean

constructor - Swift:使用指定的初始化程序覆盖便利

string - Bash 脚本 - 检查文件是否包含特定行

Cassandra 插入/更新(如果不存在或字段=值)