1

I was doing some problem in which I required to reset values in a vector of bool after some searching I found out that memset() is faster than fill() so I tried to use it like this:

vector<bool>vis(500, false);

//Some code

memset(&vis[0], false, sizeof(bool)*500);

but this is giving me compilation error:

error: taking address of rvalue [-fpermissive]

and doing this does not gives any error:

vector<int>vis(500, 0);

//Some code

memset(&vis[0], 0, sizeof(int)*500);

I don't understand this behaviour.

Some programmer dude
  • 380,411
  • 33
  • 383
  • 585
  • 8
    `vector` is a very strange beast, even by C++ standards: https://stackoverflow.com/questions/17794569/why-isnt-vectorbool-a-stl-container – Jeremy Friesner Apr 12 '21 at 16:20
  • 1
    If you want to "reset" a vector, assign such a vector to it. As in `vis = std::vector(vis.size());` – Some programmer dude Apr 12 '21 at 16:21
  • 6
    In practice, `fill` will often use `memset` (or equivalent) internally if it's safe to do so. It's dangerous to accept information like that without verifying it. – chris Apr 12 '21 at 16:23
  • 5
    [`std::vector`](https://en.cppreference.com/w/cpp/container/vector_bool) - "_The manner in which `std::vector` is made space efficient (as well as whether it is optimized at all) is implementation defined. One potential optimization involves coalescing vector elements such that each element occupies a single bit instead of `sizeof(bool)` bytes._" and "_Does not necessarily store its elements as a contiguous array._" – Ted Lyngmo Apr 12 '21 at 16:25
  • 5
    "_after some searching I found out that memset() is faster than fill()_" [citation needed]. A: Is it really? B: Is it _significantly_ faster, with enough magnitude and generality, that it justifies making your code less idiomatic and more dangerous than just using `std::fill()`? – underscore_d Apr 12 '21 at 16:26
  • 1
    You can not take the address of a bit. – Devolus Apr 12 '21 at 16:28
  • @Someprogrammerdude I don't want any work around or something I want explanation why is it working with one type and not with the other. – Sandeep Shiven Apr 12 '21 at 16:31
  • @SandeepShiven It's because it's specialized for `bool` The duplicate questions probably explains it in more detail. – Ted Lyngmo Apr 12 '21 at 16:43
  • A good read on this by Herb Sutter: [When Is a Container Not a Container?](http://www.gotw.ca/publications/mill09.htm) – Ari Apr 12 '21 at 17:11
  • `vector` is such an oddball it's almost always given its own documentation page. Often `std::bitset` proves to be a better fit, but sadly it's not dynamically sized.. – user4581301 Apr 12 '21 at 18:30
  • A last tip: Using `memset` or `memcpy` or similar lower-level C function can lead to problems in C++ programs. You have to be ***very*** careful when using them. In this specific case you can't use them. And any decent book, tutorial or class should have contained information about why `std::vector` can't be used as a "normal" vector. If you don't have access to any decent books, [here's a list of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you want to become a good C++ programmer, you really need to invest in good books. – Some programmer dude Apr 13 '21 at 05:38

0 Answers0