mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Updated Bounds.4 as suggested in issue #267
This commit is contained in:
@@ -12692,17 +12692,23 @@ These functions all have bounds-safe overloads that take `span`. Standard types
|
|||||||
void f()
|
void f()
|
||||||
{
|
{
|
||||||
array<int, 10> a, b;
|
array<int, 10> a, b;
|
||||||
memset(a.data(), 0, 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
|
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
|
##### Example, good
|
||||||
|
|
||||||
void f()
|
void f()
|
||||||
{
|
{
|
||||||
array<int, 10> a, b;
|
array<int, 10> a, b, c{}; // c is initialized to zero
|
||||||
memset(a, 0); // OK
|
a.fill(0);
|
||||||
memcmp({a, b}); // OK
|
fill(b.begin(), b.end(), 0); // std::fill()
|
||||||
|
fill(b, 0); // std::fill() + Ranges TS
|
||||||
|
|
||||||
|
if ( a == b ) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|||||||
Reference in New Issue
Block a user