mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
parent
51b4cddbca
commit
8f2ecf0665
@ -9613,7 +9613,7 @@ not. Unfortunately, it may be impossible to detect when a non-`const` was not
|
||||
|
||||
##### Reason
|
||||
|
||||
Readability.
|
||||
Readability and safety.
|
||||
|
||||
##### Example, bad
|
||||
|
||||
@ -9624,6 +9624,26 @@ Readability.
|
||||
for (i = 0; i < 200; ++i) { /* ... */ } // bad: i recycled
|
||||
}
|
||||
|
||||
##### Note
|
||||
|
||||
As an optimization, you may want to reuse a buffer as a scratchpad, but even then prefer to limit the variables's scope as much as possible and be careful not to cause bugs from data left in a recycled buffer as this is a common source of security bugs.
|
||||
|
||||
{
|
||||
std::string buffer; // to avoid reallocations on every loop iteration
|
||||
for (auto& o : objects)
|
||||
{
|
||||
// First part of the work.
|
||||
generateFirstString(buffer, o);
|
||||
writeToFile(buffer);
|
||||
|
||||
// Second part of the work.
|
||||
generateSecondString(buffer, o);
|
||||
writeToFile(buffer);
|
||||
|
||||
// etc...
|
||||
}
|
||||
}
|
||||
|
||||
##### Enforcement
|
||||
|
||||
Flag recycled variables.
|
||||
|
Loading…
x
Reference in New Issue
Block a user