{"id":5699,"date":"2019-06-05T20:19:36","date_gmt":"2019-06-05T20:19:36","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-avoid-bound-errors\/"},"modified":"2023-06-26T10:07:12","modified_gmt":"2023-06-26T10:07:12","slug":"c-core-guidelines-avoid-bound-errors","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-avoid-bound-errors\/","title":{"rendered":"C++ Core Guidelines: Avoid Bounds Errors"},"content":{"rendered":"<p>When you access an element outside a container of the STL, the result is not so promising. Your effect may be an error or undefined behavior. Undefined behavior means all bets are open.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5692\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/brno-2783268_1280.jpg\" alt=\"brno 2783268 1280\" width=\"600\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/brno-2783268_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/brno-2783268_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/brno-2783268_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/brno-2783268_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n<p>First of all: What is a bounds error? A bounds error happens when you read or write beyond the elements of a container. The result is different depending on the used container. Of course, the C++ core guidelines are very concrete.<\/p>\n<h3 id=\"slcon3-avoid-bounds-errors\"><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rsl-bounds\">SL.con.3: Avoid bounds errors<\/a><\/h3>\n<p>The C++ core guidelines start with a bad example using unsafe C-functions to fill and compare a<span style=\"font-family: courier new, courier;\"> std::array<\/span>.<\/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%;\">std::array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span> a, b;\r\nstd::memset(a.data(), <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">10<\/span>);         <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD, and contains a length error (length = 10 * sizeof(int))<\/span>\r\nstd::memcmp(a.data(), b.data(), <span style=\"color: #ff6600;\">10<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD, and contains a length error (length = 10 * sizeof(int))<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The comments to the code already say it. The length of the C-arrays is not 10 but 10 * sizeof(int). The solution is obvious. Use the functionality of the<span style=\"font-family: courier new, courier;\"> std::array<\/span>.<\/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%;\">std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span> a;\r\nstd<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span> b;\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span> c{};           \r\n\r\na.fill(<span style=\"color: #ff6600;\">0<\/span>);                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>fill(b.begin(), b.end(), <span style=\"color: #ff6600;\">0<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)  <\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> ( a <span style=\"color: #555555;\">==<\/span> b ){                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In this case, the <span style=\"font-family: courier new, courier;\">std::array a<\/span> and <span style=\"font-family: courier new, courier;\">b<\/span> are not initialized. On the opposite, all values of care are initialized to 0. Line (1) sets all values of a to 0, and line 2 uses the function templates <span style=\"font-family: courier new, courier;\">std:<\/span>:fill<span style=\"font-family: courier new, courier;\">.<\/span> Comparison is also quite convenient (line 3).<\/p>\n<p>Using a container outsides its range is, in general, undefined behavior. Let me see what that means.<\/p>\n<\/p>\n<h3>Bounds Errors<\/h3>\n<p>The most elementary sequential container we have in C++ is the C-array.<\/p>\n<h3>C-Array<\/h3>\n<p>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 next 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>Way too 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<p>Okay, what will happen if I use a sequential container from the STL? Here we are:<\/p>\n<h3>Sequential Containers of the STL<\/h3>\n<p>The index operator is available for <span style=\"font-family: courier new, courier;\">std::array, std::vector, std::deque<\/span>, and <span style=\"font-family: courier new, courier;\">std::string<\/span>. For simplicity reasons, I count a <span style=\"font-family: courier new, courier;\">std::string<\/span> as a sequential container.&nbsp;This means all containers support random access and return a random access iterator. To bore you not to death, I use only a std::array and a std::vector in my next experiment.&nbsp;<\/p>\n<h4><span style=\"font-family: courier new, courier;\">std::array<\/span><\/h4>\n<p>This is the modified program for <span style=\"font-family: courier new, courier;\">std::array<\/span>:<\/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;\">\/\/ overUnderflowStdArray.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;array&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>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span> a;\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> \r\n                       <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>Using the index operator for a C++ array is not better than using it for a C array.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5693\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdArray.png\" alt=\"overUnderflowStdArray\" width=\"400\" height=\"296\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdArray.png 870w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdArray-300x222.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdArray-768x568.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Maybe, a <span style=\"font-family: courier new, courier;\">std::vector<\/span> comes to our rescue.<\/p>\n<h4><span style=\"font-family: courier new, courier;\">std::vector<\/span><\/h4>\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;\">\/\/ overUnderflowStdVector.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;vector&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>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> a{<span style=\"color: #ff6600;\">1<\/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> \r\n                       <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>Because the <span style=\"font-family: courier new, courier;\">std::vector<\/span> creates its objects on the heap and not on the stack, such as the C- and C++-array, it takes quite a while for the program to fail. The screenshots show the beginning and the end of the under- and overflow.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignleft size-full wp-image-5694\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector1.png\" alt=\"overUnderflowStdVector1\" width=\"300\" height=\"312\" style=\"float: left;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector1.png 846w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector1-289x300.png 289w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector1-768x798.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignright size-full wp-image-5695\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector2.png\" alt=\"overUnderflowStdVector2\" width=\"300\" height=\"312\" style=\"float: right;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector2.png 846w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector2-289x300.png 289w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/overUnderflowStdVector2-768x798.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Additionally, associative containers such as <span style=\"font-family: courier new, courier;\">std::map<\/span> and <span style=\"font-family: courier new, courier;\">std::unordered_map<\/span> also support the index operator.<\/p>\n<h3>Associative Containers of the STL<\/h3>\n<p>What happens when you use a non-existing key in a <span style=\"font-family: courier new, courier;\"><\/span><span style=\"font-family: courier new, courier;\">std::map <\/span>or<span style=\"font-family: courier new, courier;\"><\/span> <span style=\"font-family: courier new, courier;\">std::unordered_map<\/span>?<\/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;\">\/\/ indexOperatorMapAndUnorderedMap.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;map&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;unordered_map&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&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>boolalpha <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>map<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string, <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myMap;\r\n    std<span style=\"color: #555555;\">::<\/span>unordered_map<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string, <span style=\"color: #007788; font-weight: bold;\">bool<\/span><span style=\"color: #555555;\">&gt;<\/span> myUnorderedMap;\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myMap[DoesNotExist]: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myMap[<span style=\"color: #cc3300;\">\"DoesNotExist\"<\/span>] <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myUnorderedMap[DoesNotExist]: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myUnorderedMap[<span style=\"color: #cc3300;\">\"DoesNotExist\"<\/span>] <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\t\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In the case of the associative container, the value you get is well-defined if the key is unavailable. The value must be DefaultConstructible because the default constructor is invoked if the key is unavailable. This creates der literal 0 in the first case and the literal <span style=\"font-family: courier new, courier;\">false<\/span> in the second case.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5696\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/indexOperatorMapAndUnorderedMap.PNG\" alt=\"indexOperatorMapAndUnorderedMap\" width=\"400\" height=\"109\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/indexOperatorMapAndUnorderedMap.PNG 1699w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/indexOperatorMapAndUnorderedMap-300x82.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/indexOperatorMapAndUnorderedMap-1024x279.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/indexOperatorMapAndUnorderedMap-768x209.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/indexOperatorMapAndUnorderedMap-1536x419.png 1536w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Okay, the essential question of the guideline remains: How can you avoid bounds errors?<\/p>\n<h3>Avoid bounds errors<\/h3>\n<p>In the case of the C-array, there is no rescue to detect a bounds error. For the C++ containers, including <span style=\"font-family: courier new, courier;\">std::string<\/span>, there is a method <span style=\"font-family: 'courier new', courier;\">at<\/span> which checks the bounds. All C++ container throws a <span style=\"font-family: courier new, courier;\">std::out_of_range<\/span> exception if you access a non-existing element. The<span style=\"font-family: courier new, courier;\"> std::string<\/span> shows this impressive.<\/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;\">\/\/ stringBoundsCheck.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;stdexcept&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&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    std<span style=\"color: #555555;\">::<\/span>string str(<span style=\"color: #cc3300;\">\"1123456789\"<\/span>); \r\n \r\n    str.at(<span style=\"color: #ff6600;\">0<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">'0'<\/span>;                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> str <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"str.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> str.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"str.capacity() = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> str.capacity() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n \r\n    try {\r\n        str.at(<span style=\"color: #ff6600;\">12<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">'X'<\/span>;                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">catch<\/span> (<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>out_of_range<span style=\"color: #555555;\">&amp;<\/span> exc) {\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> exc.what() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\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}\r\n    \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Setting the first character of the string <span style=\"font-family: courier new, courier;\">str<\/span> to &#8216;0&#8217; (line 1) is fine, but accessing a character outside the size is an error. This even holds if the access is within the capacity but outside the size of the <span style=\"font-family: courier new, courier;\">std::string<\/span>.<\/p>\n<ol>\n<li>The size of a <span style=\"font-family: courier new, courier;\">std::string<\/span> is the number of elements the <span style=\"font-family: courier new, courier;\">std::string<\/span> has.<\/li>\n<li>The capacity of a std::string is the number of elements a <span style=\"font-family: courier new, courier;\">std::string<\/span> could have without allocating additional memory.&nbsp;<\/li>\n<\/ol>\n<p>The error message of the windows compiler 19.20 is unspecific.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5697\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheckWin.PNG\" alt=\"stringBoundsCheckWin\" width=\"400\" height=\"177\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheckWin.PNG 1396w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheckWin-300x133.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheckWin-1024x453.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheckWin-768x339.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>The error message for GCC 8.2 is quite specific.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5698\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheck.png\" alt=\"stringBoundsCheck\" width=\"500\" height=\"184\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheck.png 1196w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheck-300x111.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheck-1024x378.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/06\/stringBoundsCheck-768x283.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>This post was the last one to the containers of the STL. The <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-about-strings\">next post<\/a> is about the various string types.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you access an element outside a container of the STL, the result is not so promising. Your effect may be an error or undefined behavior. Undefined behavior means all bets are open.<\/p>\n","protected":false},"author":21,"featured_media":5692,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[472,470],"class_list":["post-5699","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-associative-containers","tag-performance"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5699","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=5699"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5699\/revisions"}],"predecessor-version":[{"id":6785,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5699\/revisions\/6785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5692"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}