{"id":5406,"date":"2018-03-16T20:12:04","date_gmt":"2018-03-16T20:12:04","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-to-statements-and-arithmetic-rules\/"},"modified":"2023-06-26T11:54:41","modified_gmt":"2023-06-26T11:54:41","slug":"c-core-guidelines-rules-to-statements-and-arithmetic-rules","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-to-statements-and-arithmetic-rules\/","title":{"rendered":"C++ Core Guidelines: Rules about Statements and Arithmetic"},"content":{"rendered":"<p>Today, I will write about the remaining rules to statements and the arithmetic rules. If you don&#8217;t follow the arithmetic rules, undefined behaviour may kick in.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignright size-full wp-image-5403\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/mathematics-678969_640.png\" alt=\"mathematics 678969 640\" width=\"400\" height=\"400\" style=\"float: right;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/mathematics-678969_640.png 640w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/mathematics-678969_640-300x300.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/mathematics-678969_640-150x150.png 150w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/>&nbsp;Four rules to statements are left. Here are they:<\/p>\n<ul style=\"margin-top: 0px; margin-bottom: 1rem; color: #515151; font-family: 'PT Sans', Helvetica, Arial, sans-serif; font-size: 20px; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff;\">\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-noname\" style=\"color: #268bd2; text-decoration: none;\">ES.84: Don\u2019t (try to) declare a local variable with no name<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-empty\" style=\"color: #268bd2; text-decoration: none;\">ES.85: Make empty statements visible<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-loop-counter\" style=\"color: #268bd2; text-decoration: none;\">ES.86: Avoid modifying loop control variables inside the body of raw for-loops<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-if\" style=\"color: #268bd2; text-decoration: none;\">ES.87: Don\u2019t add redundant&nbsp;<code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">==<\/code>&nbsp;or&nbsp;<code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">!=<\/code>&nbsp;to conditions<\/a><\/li>\n<\/ul>\n<p>&nbsp;The first rule is quite obvious.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES84_Dont_try_to_declare_a_local_variable_with_no_name\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-noname\" style=\"color: #268bd2; text-decoration: none;\">ES.84: Don\u2019t (try to) declare a local variable with no name<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Declaring a local variable without a name has no effect. With the final semicolon, the variable will go out of scope.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f<\/span>()\r\n{\r\n    lock<span style=\"color: #555555;\">&lt;<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span>{mx};   <span style=\"color: #0099ff; font-style: italic;\">\/\/ Bad<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ critical region<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Typically, the optimizer can remove the creation of a temporary, if it will not change the observable behavior of the program. This is the so-called <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/as_if\">as-if<\/a> rule. To put it the other way around. If the constructor has observable behavior such as modifying the global state of the program, the optimizer is not allowed to remove the creation of the temporary.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES85_Make_empty_statements_visible\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-empty\" style=\"color: #268bd2; text-decoration: none;\">ES.85: Make empty statements visible<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To be honest, I don&#8217;t get the reason for this rule. Why do you want to write empty statements? For me, both examples are just bad.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">for<\/span> (i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> max; <span style=\"color: #555555;\">++<\/span>i);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD: the empty statement is easily overlooked<\/span>\r\nv[i] <span style=\"color: #555555;\">=<\/span> f(v[i]);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> x <span style=\"color: #555555;\">:<\/span> v) {           <span style=\"color: #0099ff; font-style: italic;\">\/\/ better<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ nothing<\/span>\r\n}\r\nv[i] <span style=\"color: #555555;\">=<\/span> f(v[i]);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES86_Avoid_modifying_loop_control_variables_inside_the_body_of_raw_for-loops\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-loop-counter\" style=\"color: #268bd2; text-decoration: none;\">ES.86: Avoid modifying loop control variables inside the body of raw for-loops<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Ok. That is, from two perspectives, very bad practice. First, you should avoid writing raw loops and use the algorithms of the Standard Template Library. Second, you should not modify the control variable inside the for-loop. Here is the wrong practice.&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">10<\/span>; <span style=\"color: #555555;\">++<\/span>i) {\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #0099ff; font-style: italic;\">\/* something *\/<\/span>) <span style=\"color: #555555;\">++<\/span>i; <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/<\/span>\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> skip <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">10<\/span>; <span style=\"color: #555555;\">++<\/span>i) {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (skip) { skip <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">false<\/span>; <span style=\"color: #006699; font-weight: bold;\">continue<\/span>; }\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #0099ff; font-style: italic;\">\/* something *\/<\/span>) skip <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">true<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ Better: using two variable for two concepts.<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>What makes it difficult for me to reason in particular about the second for-loop is that these are under the hood two nested dependent loops.&nbsp;&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES87_Dont_add_redundant_or_to_conditions\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-if\" style=\"color: #268bd2; text-decoration: none;\">ES.87: Don\u2019t add redundant&nbsp;<code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">==<\/code>&nbsp;or&nbsp;<code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">!=<\/code>&nbsp;to conditions<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;I&#8217;m guilty. In my first years as a professional C++ developer, I often used redundant <span style=\"font-family: 'courier new', courier;\">==<\/span> or<span style=\"font-family: 'courier new', courier;\"> !=<\/span>&nbsp;in conditions. Of course, this changed in the meantime.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ p is not a nullptr<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (p) { ... }            <span style=\"color: #0099ff; font-style: italic;\">\/\/ good<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (p <span style=\"color: #555555;\">!=<\/span> nullptr) { ... } <span style=\"color: #0099ff; font-style: italic;\">\/\/ redundant <\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ p is a nullptr<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #555555;\">!<\/span>p) { ... }           <span style=\"color: #0099ff; font-style: italic;\">\/\/ good<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (p <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span>) { ... }       <span style=\"color: #0099ff; font-style: italic;\">\/\/ redundant <\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">for<\/span> (string s; cin <span style=\"color: #555555;\">&gt;&gt;<\/span> s;)  <span style=\"color: #0099ff; font-style: italic;\">\/\/ the istream operator returns bool<\/span>\r\nv.push_back(s);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>These were the rules to statements. Let&#8217;s continue with the arithmetic rules. Here are the first seven.<\/p>\n<ul style=\"margin-top: 0px; margin-bottom: 1rem; color: #515151; font-family: 'PT Sans', Helvetica, Arial, sans-serif; font-size: 20px; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: #ffffff;\">\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-mix\" style=\"color: #268bd2; text-decoration: none;\">ES.100: Don\u2019t mix signed and unsigned arithmetic<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-unsigned\" style=\"color: #268bd2; text-decoration: none;\">ES.101: Use unsigned types for bit manipulation<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-signed\" style=\"color: #268bd2; text-decoration: none;\">ES.102: Use signed types for arithmetic<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-overflow\" style=\"color: #268bd2; text-decoration: none;\">ES.103: Don\u2019t overflow<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-overflow\" style=\"color: #268bd2; text-decoration: none;\"><\/a><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-underflow\" style=\"color: #268bd2; text-decoration: none;\">ES.104: Don\u2019t underflow<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-underflow\" style=\"color: #268bd2; text-decoration: none;\"><\/a><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-zero\" style=\"color: #268bd2; text-decoration: none;\">ES.105: Don\u2019t divide by zero<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-nonnegative\" style=\"color: #268bd2; text-decoration: none;\"><span style=\"color: #268bd2;\">ES.106: Don\u2019t try to avoid negative values by using&nbsp;<\/span><code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">unsigned<\/code><\/a><\/li>\n<\/ul>\n<p>Honestly, there is often not much for me to add to these rules. For completeness (and importance), I will briefly present the rules.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES100_Dont_mix_signed_and_unsigned_arithmetic\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-mix\" style=\"color: #268bd2; text-decoration: none;\">ES.100: Don\u2019t mix signed and unsigned arithmetic<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You will not get the expected result if you mix signed and unsigned arithmetic.&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">3<\/span>;\r\n  <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">-<\/span> y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4294967286<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">+<\/span> y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">*<\/span> y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4294967275<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">\/<\/span> y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 613566756<\/span>\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;GCC, Clang, and Microsoft Compiler produced the same results.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES101_Use_unsigned_types_for_bit_manipulation\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-unsigned\" style=\"color: #268bd2; text-decoration: none;\">ES.101: Use unsigned types for bit manipulation<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The reason for the rules is quite simple. Bitwise operations on signed types are implementation-defined.&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES102_Use_signed_types_for_arithmetic\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-signed\" style=\"color: #268bd2; text-decoration: none;\">ES.102: Use signed types for arithmetic<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>First, you should do arithmetic with signed types. Second, you should not mix signed and unsigned arithmetic. If not, the results may surprise you.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>\r\nT subtract(T x, T2 y){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> x <span style=\"color: #555555;\">-<\/span> y;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> s <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> us <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> subtract(s, <span style=\"color: #ff6600;\">7<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ -2<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> subtract(us, <span style=\"color: #ff6600;\">7u<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4294967294<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> subtract(s, <span style=\"color: #ff6600;\">7u<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ -2<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> subtract(us, <span style=\"color: #ff6600;\">7<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4294967294<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> subtract(s, us <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ -2<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> subtract(us, s <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4294967294<\/span>\r\n\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES103_Dont_overflow_and_ES104_Dont_underflow\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-overflow\" style=\"color: #268bd2; text-decoration: none;\">ES.103: Don\u2019t overflow<\/a>, and&nbsp;<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-underflow\" style=\"color: #268bd2; text-decoration: none;\">ES.104: Don\u2019t underflow<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Let me combine both rules. The effect of an overflow or an underflow is the same: memory corruption and undefined behavior. Let&#8217;s make a simple test with an <span style=\"font-family: 'courier new', courier;\">int<\/span> array. How long will the following program run?<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ overUnderflow.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstddef&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> a[<span style=\"color: #ff6600;\">0<\/span>];\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n{};\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">while<\/span> (<span style=\"color: #336666;\">true<\/span>){\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #555555;\">!<\/span>(n <span style=\"color: #555555;\">%<\/span> <span style=\"color: #ff6600;\">100<\/span>)){\r\n            std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"a[\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> n <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"] = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> a[n] <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", a[\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">-<\/span>n <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"] = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> a[<span style=\"color: #555555;\">-<\/span>n] <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n        }\r\n        a[n] <span style=\"color: #555555;\">=<\/span> n;\r\n        a[<span style=\"color: #555555;\">-<\/span>n] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span>n;\r\n        <span style=\"color: #555555;\">++<\/span>n;\r\n    }\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Disturbing long. The program writes each 100th array entry to <span style=\"font-family: 'courier new', courier;\">std::cout.<\/span>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5404\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/overUnderflow.png\" alt=\"overUnderflow\" width=\"350\" height=\"395\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/overUnderflow.png 468w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/overUnderflow-266x300.png 266w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES105_Dont_divide_by_zero\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-zero\">ES.105: Don&#8217;t divide by zero<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you want to have a crash, you should divide by zero. Diving by zero may be fine in a logical expression.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">bool<\/span> res <span style=\"color: #555555;\">=<\/span> <span style=\"color: #336666;\">false<\/span> and (<span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">0<\/span>);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Because the result of the expression (<span style=\"font-family: 'courier new', courier;\">1\/0<\/span>) is not necessary for the overall result, it will not be evaluated. This technique is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Short-circuit_evaluation\">short circuit evaluation<\/a> and is a particular case of lazy evaluation.&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ES106_Dont_try_to_avoid_negative_values_by_using_unsigned\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-nonnegative\" style=\"color: #268bd2; text-decoration: none;\">ES.106: Don\u2019t try to avoid negative values by using&nbsp;<code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">unsigned<\/code><\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Don&#8217;t use an&nbsp;unsigned type if you want to avoid negative values. The consequences may be severe. Arithmetic behavior will change, and you are open to errors, including signed\/unsigned arithmetic.<\/p>\n<p>Here are two examples of the Guidelines, intermixing signed\/unsigned arithmetic.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> u1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span>;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ Valid: the value of u1 is 4294967294<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> i1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span>;\r\n<span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> u2 <span style=\"color: #555555;\">=<\/span> i1;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ Valid: the value of u2 is 4294967294<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> i2 <span style=\"color: #555555;\">=<\/span> u2;            <span style=\"color: #0099ff; font-style: italic;\">\/\/ Valid: the value of i2 is -2<\/span>\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #cc00ff;\">area<\/span>(<span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> height, <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> width) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> height<span style=\"color: #555555;\">*<\/span>width; } \r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> height;\r\ncin <span style=\"color: #555555;\">&gt;&gt;<\/span> height;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> a <span style=\"color: #555555;\">=<\/span> area(height, <span style=\"color: #ff6600;\">2<\/span>);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ if the input is -2 a becomes 4294967292<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>As the Guidelines stated, there is an interesting relationship. When you assign a -1 to an <span style=\"font-family: 'courier new', courier;\">unsigned int<\/span>, you will become the largest <span style=\"font-family: 'courier new', courier;\">unsigned int<\/span>.<\/p>\n<p>Now to the more interesting case. The behavior of arithmetic will differ between&nbsp;signed and unsigned types.<\/p>\n<p>Let&#8217;s start with a simple program.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ modulo.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstddef&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> max{<span style=\"color: #ff6600;\">100000<\/span>};    \r\n    <span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">short<\/span> x{<span style=\"color: #ff6600;\">0<\/span>};                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> count{<span style=\"color: #ff6600;\">0<\/span>};\r\n    <span style=\"color: #006699; font-weight: bold;\">while<\/span> (x <span style=\"color: #555555;\">&lt;<\/span> max <span style=\"color: #555555;\">&amp;&amp;<\/span> count <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">20<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;           \r\n        x <span style=\"color: #555555;\">+=<\/span> <span style=\"color: #ff6600;\">10000<\/span>;                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #555555;\">++<\/span>count;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The key point of the program is that the successive addition to x inline (1) will not trigger an overflow but a modulo operation if the value range of x ends. The reason is that x is of <span style=\"font-family: 'courier new', courier;\">unsigned short<\/span> (2) type.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ overflow.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstddef&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> max{<span style=\"color: #ff6600;\">100000<\/span>};    \r\n    <span style=\"color: #007788; font-weight: bold;\">short<\/span> x{<span style=\"color: #ff6600;\">0<\/span>};                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> count{<span style=\"color: #ff6600;\">0<\/span>};\r\n    <span style=\"color: #006699; font-weight: bold;\">while<\/span> (x <span style=\"color: #555555;\">&lt;<\/span> max <span style=\"color: #555555;\">&amp;&amp;<\/span> count <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">20<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n        x <span style=\"color: #555555;\">+=<\/span> <span style=\"color: #ff6600;\">10000<\/span>;                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #555555;\">++<\/span>count;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I made a slight change to the program <span style=\"font-family: 'courier new', courier;\">modulo.cpp<\/span> such that x (2) becomes a signed type. The same addition will now trigger an overflow.<\/p>\n<p>I marked the key points with red circles in the screenshot.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5405\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/ModuloOverflow.png\" alt=\"ModuloOverflow\" width=\"600\" height=\"148\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/ModuloOverflow.png 1178w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/ModuloOverflow-300x74.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/ModuloOverflow-1024x252.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/03\/ModuloOverflow-768x189.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>I have a burning question: How can I detect an overflow? Quite easy. Replace the erroneous assignment <span style=\"font-family: 'courier new', courier;\">x += 1000;<\/span> with an expression using curly braces: <span style=\"font-family: 'courier new', courier;\">x = {x + 1000}<\/span>;. The difference is that the compiler checks narrowing conversions and detects the overflow. Here is the output from GCC.&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5383\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/narrowingConversion.png\" alt=\"narrowingConversion\" width=\"600\" height=\"109\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Sure, the expressions (<span style=\"font-family: 'courier new', courier;\">x += 1000<\/span>) and (<span style=\"font-family: 'courier new', courier;\">x&nbsp; = {x + 1000}<\/span>) are, from a performance perspective, not the same. The second one could create a temporary for <span style=\"font-family: 'courier new', courier;\">x + 1000<\/span>. But in this case, the optimizer did a great job, and both expressions were under the hood the same.&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>I&#8217;m nearly done with the arithmetic rules. This means in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-to-performance\">next post<\/a> I will continue my journey with the rules to performance.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I will write about the remaining rules to statements and the arithmetic rules. If you don&#8217;t follow the arithmetic rules, undefined behaviour may kick in.<\/p>\n","protected":false},"author":21,"featured_media":5403,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[488],"class_list":["post-5406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-arithmetic"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5406"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5406\/revisions"}],"predecessor-version":[{"id":6835,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5406\/revisions\/6835"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5403"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}