Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to memset an array of bools?

Tags:

c++

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."

like image 993
user173438 Avatar asked Feb 09 '12 16:02

user173438


2 Answers

//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.

like image 189
Siva Prakash Avatar answered Oct 05 '22 11:10

Siva Prakash


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);
like image 20
wilhelmtell Avatar answered Oct 05 '22 12:10

wilhelmtell