How to memset an array of bools?

前端 未结 6 1077
心在旅途
心在旅途 2021-02-20 13:25
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 bool

相关标签:
6条回答
  • 2021-02-20 14:00

    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 讨论(0)
  • 2021-02-20 14:10

    Simply like this example:

        bool primes[MAX];
        memset(primes,true,sizeof(primes));
    
    0 讨论(0)
  • 2021-02-20 14:13
    memset(buffer_start, value, sizeof(bool) * number_of_bools);
    
    0 讨论(0)
  • 2021-02-20 14:16

    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);
    
    0 讨论(0)
  • 2021-02-20 14:18
    //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 讨论(0)
  • 2021-02-20 14:20

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

    std::fill(std::begin(bArray), std::end(bArray), value);
    
    0 讨论(0)
提交回复
热议问题