C++核心准则ES.104:防止下溢

ES.104: Don't underflow
ES.104:防止下溢
Reason(原因)
Decrementing a value beyond a minimum value can lead to memory corruption and undefined behavior.
减少一个值越过最小值,可以引起内存破坏和无定义行为。
Example, bad(反面示例)
int a[10];
a[-2] = 7; // bad
int n = 101;
while (n--)
a[n - 1] = 9; // bad (twice)
Exception(例外)
Use unsigned types if you really want modulo arithmetic.
如果确实需要按模运算,可以使用无符号类型。
Enforcement(实施建议)
???
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es104-dont-underflow
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
评论
