Initialize memory with nan in C ++ for debugging

How to initialize all memory in a c or C ++ program for NaN (Not-A-Number) at startup for debugging using gdb?

I believe that by default gdb is initialized to zeros, but this often does not help to find code that crashes due to an initialization error.

PS: I want to initialize each variable and array as NAN (or some garbage) for debugging only. The program I'm working with has thousands of variables, so it’s rather tedious to change each ...

+4
source share
3 answers

These hexadecimal numbers may be correct in the Rafael post, but I would recommend a more semantic way.

. http://en.cppreference.com/w/cpp/types/numeric_limits/quiet_NaN

#include <limits>
double nan1 = std::numeric_limits<double>::quiet_NaN();
double nan2 = std::numeric_limits<double>::signaling_NaN();

, NaN.

+4

32- ints

0x7FC00000 and 0x7FFFFFFF or 
0xFFC00000 and 0xFFFFFFFF

u64

0x7FF8000000000000 and 0x7FFFFFFFFFFFFFFF or 
0xFFF8000000000000 and 0xFFFFFFFFFFFFFFFF
+2

" "? , , Linux. gdb .

: , , , . , , . . .

( C), , NaN. . , int, 0, NaN .

If you need variables or an array initialized to NaN, just initialize the variables accordingly when they are declared (as explained by Notinlist and Rafael). You can use some macros if you really don't want to repeat this ugly long statement every time, something like

#define NaNdouble(X) double X = std::numeric_limits<double>::quiet_NaN();
+1
source

All Articles