10
void *memset(void *dest, int c, size_t count)

The 3rd argument is the Number of characters or bytes in the array. How would you memset an array of booleans, say bool bArray[11]?

MSDN says: "Security Note - Make sure that the destination buffer has enough room for at least count characters."

6
  • 2
    bool bArray[11] = {}? Or std::fill(begin(bArray), end(bArray), true)? Feb 9, 2012 at 17:00
  • 11 * sizeof(bool). That being said, that's not very c++; you should be using std::fill() Feb 9, 2012 at 17:01
  • 1
    Seems vector<bool> is optimized for space allocation and is a 'real' array of bits. A C++ style array of bools is essentially an array of bytes with all bits either 0 or 1. Vector<bool> is the way to go. Thanks for all the responses. Much appreciated.
    – user173438
    Feb 9, 2012 at 17:11
  • 1
    @user173438: vector<bool> is slow. You probably wanted std::bitset. Feb 9, 2012 at 17:28
  • 1
    @user173438: vector<bool> is also (surprisingly) not a normal container, unlike vector. See explanation here: books.google.de/… Feb 10, 2012 at 13:40

6 Answers 6

20

std::fill() should use memset() when possible.

std::fill(std::begin(bArray), std::end(bArray), value);

If bArray is a pointer, then the following should be used:

std::fill(bArray, bArray + arraySize, value);
3
  • 3
    If he gets bArray via a pointer, how can you get std::end(bArray)?
    – einpoklum
    Nov 20, 2013 at 14:11
  • 1
    @einpoklum If bArray is a pointer, this line should be std::fill(bArray, bArray + arraySize, value);
    – MewX
    Jun 24, 2017 at 16:03
  • 2
    @MewX: Obviously, but that's not what this answer says right now.
    – einpoklum
    Jun 24, 2017 at 21:58
5
memset(buffer_start, value, sizeof(bool) * number_of_bools);
4
  • sizeof(bool) makes little sense there - memset just fills each byte with a bool. Feb 10, 2012 at 13:36
  • 9
    @blaisorblade Yes sizeof(bool) == 1, but clearly stating your intent in code is never a bad thing.
    – Gigi
    Feb 10, 2012 at 17:24
  • 10
    Size of bool may not be 1.
    – Neil Kirk
    Oct 28, 2015 at 0:07
  • 1
    Elaborating on what Neil said, as per 5.3.3/1: "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined." Aug 13, 2018 at 17:29
3
//Array declaration
bool arr[10];

//To initialize all the elements to true

memset(arr,1,sizeof(arr));

Similarly, you can initialize all the elements to false, by replacing 1 with 0.

0

memset sets memory in multiples of bytes. So, the only way is to add padding to your bool pointer such that its length is a multiple of 8. Then do memset. Personally I would prefer if there were any other alternative than putting a redundant padding. But I haven't found any alternative solution to date.

0

Simply like this example:

    bool primes[MAX];
    memset(primes,true,sizeof(bool) * MAX);
-1

To set array of 11 bool elements to e.g. true by using memset:

const int N = 11;
bool arr[N];
memset(arr, 1, sizeof(bool) * N);
1
  • 7
    It should be memset(arr, 1, sizeof(bool) * N);
    – miloszmaki
    Nov 21, 2012 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.