uninitialised value(s) - if_nan function

75 views Asked by At

I have a code that calls the following function

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

to find if_nan() is encountered in calculations. When I do memcheck with Valgrind, I get the following error:

 ==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)
...

I don't understand what value to be initialized here. Please suggest a way to avoid this error. Thank You

1

There are 1 answers

0
Sourav Ghosh On

And we don't understand what you want from us by posting 1/100th of your code !!!

Howver, check the below part for your clarification. And please , from next time, try to put the entire code here. Its free of cost. :-)

Case 1

code

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

Case 2

Code

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

Looks familiar? :-) [No hard feelings]