mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Updated Bounds.4 as suggested in issue #267
This commit is contained in:
parent
d81adf4a8e
commit
7e81a238e0
|
@ -12692,17 +12692,23 @@ These functions all have bounds-safe overloads that take `span`. Standard types
|
|||
void f()
|
||||
{
|
||||
array<int, 10> a, b;
|
||||
memset(a.data(), 0, 10); // BAD, and contains a length error
|
||||
memcmp(a.data(), b.data(), 10); // BAD, and contains a length error
|
||||
memset(a.data(), 0, 10); // BAD, and contains a length error (length = 10 * sizeof(int))
|
||||
memcmp(a.data(), b.data(), 10); // BAD, and contains a length error (length = 10 * sizeof(int))
|
||||
}
|
||||
|
||||
Also, `std::array<>::fill()` or `std::fill()` or even an empty initializer are better candidate than `memset()`.
|
||||
|
||||
##### Example, good
|
||||
|
||||
void f()
|
||||
{
|
||||
array<int, 10> a, b;
|
||||
memset(a, 0); // OK
|
||||
memcmp({a, b}); // OK
|
||||
array<int, 10> a, b, c{}; // c is initialized to zero
|
||||
a.fill(0);
|
||||
fill(b.begin(), b.end(), 0); // std::fill()
|
||||
fill(b, 0); // std::fill() + Ranges TS
|
||||
|
||||
if ( a == b ) {
|
||||
}
|
||||
}
|
||||
|
||||
##### Example
|
||||
|
|
Loading…
Reference in New Issue
Block a user