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."
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);
memset(buffer_start, value, sizeof(bool) * number_of_bools);
//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.
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.
Simply like this example:
bool primes[MAX];
memset(primes,true,sizeof(bool) * MAX);
To set array of 11 bool elements to eg true by using memset
:
const int N = 11;
bool arr[N];
memset(arr, 1, sizeof(bool) * N);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.