2

I have this simple program:

test.cc:

#include <limits>
#include <iostream>
#include <cmath>
int main() {
    std::cout << (std::isinf(std::numeric_limits<long double>::infinity()) ? "true" : "false") << std::endl;
}

When I run it normally, the result is as expected:

g++ -std=c++17 -Wall -Wextra -pedantic -O0 -g3 test.cc && ./a.out:

true

However, when I run it with valgrind, it instead prints false.

g++ -std=c++17 -Wall -Wextra -pedantic -O0 -g3 test.cc && valgrind --leak-check=full -- ./a.out:

==2680== Memcheck, a memory error detector
==2680== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2680== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2680== Command: ./a.out
==2680==
false
==2680==
==2680== HEAP SUMMARY:
==2680==     in use at exit: 0 bytes in 0 blocks
==2680==   total heap usage: 2 allocs, 2 frees, 73,728 bytes allocated
==2680==
==2680== All heap blocks were freed -- no leaks are possible
==2680==
==2680== For lists of detected and suppressed errors, rerun with: -s
==2680== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Why does this happen?

(I use "g++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0" and "valgrind-3.15.0".)

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • 3
    valgrind does not support long double. Does https://stackoverflow.com/questions/44102050/valgrind-leading-to-numeric-issues-with-long-doubles answer your question? – KamilCuk Feb 10 '23 at 09:25
  • @KamilCuk This seems to be the problem. That other question doesn't have any solutions, though. – Piotr Siupa Feb 10 '23 at 09:30
  • While Valgrind is useful, don't use it as your only tool. Since you're using GCC it have *sanitizers* which can also be used. For example there are both memory errors and leak sanitizers. See the `-fsanitize` options in [the documentation](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html). But again, don't use them as the only tools, *also* use Valgrind, but be aware of its shortcomings (as well as the shortcomings of the sanitizers), and let the different tools *complement* each other. – Some programmer dude Feb 10 '23 at 09:38
  • @Someprogrammerdude Well, the problem now is that I cannot use valgrind either as the only tool or as a complementary tool, because it breaks my program. – Piotr Siupa Feb 10 '23 at 09:57
  • Valgrind can still be used for testing *other* things, even if it's not testing all the code-paths that you might expect. :) – Some programmer dude Feb 10 '23 at 10:00
  • 1
    You may force 64 bit `long double` types with `-mlong-double-64` GCC option. – ks1322 Feb 10 '23 at 10:05
  • @ks1322 This works. This way `long double` is not properly tested with valgrind but I can run tests again without it to check whenever algorithms for 80-bit `long double` work. – Piotr Siupa Feb 10 '23 at 10:23
  • There are a lot of issues with floating point support in Valgrind. One of my pipe dreams is to get FPE working (it's often a blocking problem for us to use Valgrind at work). Fully correct floating point emulation would require a large amount of work. – Paul Floyd Feb 10 '23 at 14:28

0 Answers0