I want to set all the index value to -1
in a double array.
Here is my code :
double dp[505];
memset(dp,-1,sizeof(dp));
cout<<dp[0]<<"\n";
But it is showing nan
when i try to print its value.
What does nan
mean? Is it possible to use memset()
in double array?
memset
operates on bytes, not floats, and a double
with all bytes set to -1 does not equal -1. I think you're looking for std::fill
:
#include <algorithm>
std::fill(dp, dp + 505, -1.0);
Or, in C++11:
#include <algorithm>
#include <iterator>
std::fill(std::begin(dp), std::end(dp), -1.0);
In C++, you can write:
double initValue = -1;
std::fill_n(dp, 505, initValue);
memsetting a double array with a non-double value won't work.
You have set each element of the array to be filled with the byte 0xFF
(ie the char
representation of -1
).
No floating point number is represented by a series of 0xFF
bytes, so on printing the double
, you see NaN
(ie 'not a number'). This is in apparent contrast to memset
'ting the bytes to zero, which is legal as a string of 0 bytes is a double
with value zero. See Is it legal to use memset(,0,) on array of doubles? .
If you meant to set every entry to -1.0
(ie a double), then use std::fill
or std::fill_n
in C++ or a loop in C, eg
int n;
for (n = 0 ; n < 505 ; n++)
dp[n] = -1.0;
From the man page of memset
:
The
memset()
function fills the first n bytes of the memory area pointed to by s with the constant byte c .
The problem is that you want to fill an array of double
s with the constant -1.0
but sizeof(double) > 1
so memset
actually fills in garbage which happens to end up as a NaN .
If you are using C++, the std::fill
function is your friend. Actually, since you are writing to your array for the first time, std::uninitialized_fill
would be correct. Although for the builtin double
type there should be no difference but it is always good to be precise.
constexpr std::size_t length = 505;
double values[length];
std::uninitialized_fill(values, values + length, -1.0);
memset
sets bytes, so you get double
-values where each byte is -1
.
Instead in C++ use std::vector
, then write
vector<double> dp( 505, -1.0 );
It's that simple.
If dp
is a global vector and you need to set it to -1
a number of times, then you can simply do this:
dp = vector<double>( dp.size(), -1.0 );
However, it's generally not a good idea to use non- const
global variables .
Alternatively one can use std::fill
, or just a loop, or just about any technique that still treat the double
values as double
values. But std::vector
is preferable also for many other reasons than greatly simplifying the fill-it task. In particular a std::vector
can be resized, it takes care of copying, and it automates the memory management, doing that part correctly and transparent to you.
nan means not a number.
cant see why its not working. maybe because precision is not set : (cout.precision(15);)
check this: How do I print a double value with full precision using cout?
But im not sure at all it will works :oi checked memset source code and there's no problem with negative :D
But it can be a problem with doubles :
memset(dst0, c0, length) void *dst0;
register int c0;
register size_t length;
Have you tried to compile with Werror flag ?
Although answers for you question have been given, I just wanted you to note that sizeof(dp)
outputs the number of bytes used to code the variable in memory.
In your case, dp is a pointer to a double. It will then be equal to the size of a pointer (4), no matter wether or not memery has been allocated. sizeof(*dp) will output the size of a double (8). In order to use the length of a
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.