-5

When i'm trying to use fill() like following way, it causing me error :

double dp[165][2];
fill(dp, dp+165*2, -1.0);

error: macro "fill" passed 3 arguments, but takes just 2

It also causing error when i'm trying with fill_n() :

double dp[165][2];
fill_n(dp, 165*2, -1.0);

error: incompatible types in assignment of 'const double' to 'double [2]'

can you point out the problem here ?

Ali Akber
  • 3,670
  • 3
  • 26
  • 40
  • 4
    What's `fill()` actually? – πάντα ῥεῖ Dec 20 '14 at 16:56
  • From the error, you can deduce that the fill method accepts only two parameters. – Nick Dec 20 '14 at 16:56
  • @πάντα it will set a particular value to every index of the array – Ali Akber Dec 20 '14 at 16:57
  • 1
    @AliAkber _"it will set a particular value to every index of the array"_ If you use [`std::fill()`](http://en.cppreference.com/w/cpp/algorithm/fill), may be yes. – πάντα ῥεῖ Dec 20 '14 at 16:59
  • i use `using namespace std;` after the header. – Ali Akber Dec 20 '14 at 17:00
  • @AliAkber _"i use using `namespace std;` ..."_ That's one thing you never should do, it bites you in the back. – πάντα ῥεῖ Dec 20 '14 at 17:01
  • why ? can you explain please – Ali Akber Dec 20 '14 at 17:01
  • 1
    @AliAkber [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/q/1452721/1870232) – P0W Dec 20 '14 at 17:03
  • Thanks :) i didn't aware of the fact before – Ali Akber Dec 20 '14 at 17:04
  • @kec That's not true, it'll still invoke the macro. Macros aren't namespace sensitive. – Barry Dec 20 '14 at 17:07
  • Note that in practice, just be careful. Is the world going to end if you put it into your small test program's .cpp file? No, but you might run into problems such as you did. If you are an expert C++ programmer who is also a bad typist, you'll be able to resolve such issues quickly, and thus it'd probably be okay for you. At the other extreme, putting this in a header file for a large, widely used library would be a disaster. – kec Dec 20 '14 at 17:08
  • @Barry: I deleted it. – kec Dec 20 '14 at 17:09
  • 1
    can i know, why i'm getting downvote please ? shouldn't i ask the solution of a problem i'm facing ? – Ali Akber Dec 20 '14 at 17:12
  • Well, I don't know why you are getting downvoted, but the answer that you've received does answer your question. – kec Dec 20 '14 at 17:33
  • yes. of course.. i accepted it.. but getting downvote really disappointed me :( – Ali Akber Dec 20 '14 at 17:35
  • I didn't downvote, but some interpret basic questions as reflecting a lack of effort. That'd be my guess. – kec Dec 20 '14 at 18:05
  • @kec i know you didn't. It's okay. Actually i learn about fill() and fill_n() today. so i'm at basic level in this – Ali Akber Dec 20 '14 at 18:25

1 Answers1

3

Either

std::fill(dp[0], dp[0] + 165 * 2, -1.0);

or

std::fill_n(dp[0], 165 * 2, -1.0);

should work in standard C++. The error about fill macro with two arguments suggests that something #defines a macro named fill that interferes with the C++ standard library, though. This is likely to break more things than just this, so you should look into it and see if there's something like the NOMINMAX macro that prevents windows.h from doing the same with min and max (which breaks <limits>).

The problem you run into is that the value type ofdp is not double but double[2], to which -1.0 cannot be assigned. The above code uses the fact that a 2D array of doubles looks in memory just like a flat array of doubles, which means that by casting dp to double*, it can be filled just like a 1D array. dp[0] is just the easiest way to get a pointer to the first element of dp of type double*.

Addendum: Technically, dp[0] is of type double[2], but this decays to double* when it is used. dp can only decay to double (*)[2] (pointer to array of two doubles).

Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • Although there is an issue with iterating beyond one past the end of an array, which technically is undefined behaviour. So this code is UB, although it is difficult to imagine an implementation in which it wouldn't work. – juanchopanza Dec 20 '14 at 18:39
  • There's a special dispensation in the standard for this scenario (e.g. 5.7 (7) in C++03); a pointer to one element past the end of an array will compare in the expected way to a pointer to an element in the array. The behavior is well-defined. – Wintermute Dec 20 '14 at 18:51
  • One past is fine, two past isn't. – juanchopanza Dec 20 '14 at 18:52
  • But this code doesn't go two elements past the end of any array. (side note: correction: it's 3.9.2 (3); 5.7 (7) defines something related but different) – Wintermute Dec 20 '14 at 18:54
  • The length of `dp` is 165. There's a related post [here](https://stackoverflow.com/questions/6015080/c-c-is-this-undefined-behavior-2d-arrays). – juanchopanza Dec 20 '14 at 23:04
  • Yes, but the members of `dp` contain a combined 165 * 2 doubles. The trick here to look at `dp[0]` as an array of 165 * 2 doubles, and `dp[0] + 165 * 2` is one past the end of that. – Wintermute Dec 21 '14 at 00:02
  • The point is that doing that is UB. – juanchopanza Dec 21 '14 at 07:55