3

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?

SSC
  • 1,311
  • 5
  • 18
  • 29
Ali Akber
  • 3,670
  • 3
  • 26
  • 40

7 Answers7

14

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.

erenon
  • 18,838
  • 2
  • 61
  • 93
7

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);
Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • what can i do if dp is 2D array ? – Ali Akber Dec 20 '14 at 15:15
  • You can interpret the 2D array as a flat array of height * width doubles (it looks the same in memory) and write `std::fill(arr2d[0], arr2d[0] + width * height, -1.0);` – Wintermute Dec 20 '14 at 16:42
  • when i'm trying to use fill(), it causing me error `error: macro "fill" passed 3 arguments, but takes just 2` but it works when i'm using @erenon's way – Ali Akber Dec 20 '14 at 16:46
  • `fill` is not a macro. Probably you're using some other library that `#define`s a `fill()` macro that interferes with this. – Wintermute Dec 20 '14 at 16:48
  • what can i do to make it work ? http://stackoverflow.com/questions/27582333/error-in-using-fill-and-fill-n-in-2d-double-type-array – Ali Akber Dec 20 '14 at 16:55
5

You have set each element of the array to be filled with the byte 0xFF (i.e. 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 (i.e. '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 (i.e. a double), then use std::fill or std::fill_n in C++ or a loop in C, e.g.

int n;
for (n = 0 ; n < 505 ; n++)
    dp[n] = -1.0;
Community
  • 1
  • 1
abligh
  • 24,573
  • 4
  • 47
  • 84
4

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 doubles 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);
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
2

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.

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • i know it. But what if dp is a global vector and i need to set dp values -1 a certain number of times – Ali Akber Dec 20 '14 at 15:01
  • 2
    I'm no C++ guru, but I'd have written: `vector dp( 505, -1.0 );` – abligh Dec 20 '14 at 15:01
  • @abligh: thanks, that may be more clear yes. improved. – Cheers and hth. - Alf Dec 20 '14 at 15:03
  • @abligh you are right. I think it saves time. because if i do as the answer says, there should be some time waste to convert decimal to float – Ali Akber Dec 20 '14 at 15:03
  • @AliAkber: No there's no runtime cost (to convert from `-1` to `-1.0`), but there could be a programmer's time cost. – Cheers and hth. - Alf Dec 20 '14 at 15:04
  • @Cheers what do you mean by programmer's time cost? – Ali Akber Dec 20 '14 at 15:05
  • @AliAkber: I mean, someone not seeing at a glance what the declaration means. Maybe someone wasting time correcting the declaration. About 80% of programming is maintenance (if I recall the stats correctly, may have changed a little), so it's wise to write code that communicates as clearly as possible to maintenance programmers. – Cheers and hth. - Alf Dec 20 '14 at 15:07
  • @Cheers it can be .. let me say one thing how can i do this if dp is global array and i need to do it a number of times ? – Ali Akber Dec 20 '14 at 15:11
0

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 :o i 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 ?

Community
  • 1
  • 1
-1

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

Mael
  • 1
  • Wrong. "When sizeof is applied to the name of an array, the result is the number of bytes required to store the entire array". – Michel Rouzic Oct 24 '17 at 15:11