{"id":5781,"date":"2019-09-26T14:10:45","date_gmt":"2019-09-26T14:10:45","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-bounds-safety\/"},"modified":"2023-06-26T10:00:53","modified_gmt":"2023-06-26T10:00:53","slug":"c-core-guidelines-bounds-safety","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-bounds-safety\/","title":{"rendered":"C++ Core Guidelines: Bounds Safety"},"content":{"rendered":"<p>Today&#8217;s post concerns the second C++ Core Guidelines: Bounds Safety profile. The goal of the profile bounds safety is that you operate inside the bounds of allocated memory.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5780\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/industrsy.jpg\" alt=\"industrsy\" width=\"500\" height=\"333\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/industrsy.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/industrsy-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/industrsy-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/09\/industrsy-768x512.jpg 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The profile names the two enemies for bounds safety: pointer arithmetic and array indexing. Additionally, when you use a pointer, it should only address a single object but not an array. To complete the profile bounds safety, you should combine it with the rules to type safety and lifetime safety. Type safety was the topic of my two previous posts: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-profiles-type-safety-bounds-safety-and-lifetime-safety\">C++ Core Guidelines: Type Safety<\/a> and <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-type-safety-per-design\">C++ Core Guidelines: Type Safety by Design<\/a>. Lifetime safety will be the topic of my next post.<\/p>\n<h3><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#SS-bounds\">Bounds Safety<\/a><\/h3>\n<p>Bounds safety consists of four rules:<\/p>\n<ul>\n<li>Bounds.1: Don\u2019t use pointer arithmetic<code><\/code><code class=\"highlighter-rouge no-highlight\"><\/code><\/li>\n<li>Bounds.2: The only index into arrays using constant expressions<\/li>\n<li>Bounds.3: No array-to-pointer decay<\/li>\n<li>Bounds.4: Don\u2019t use standard-library functions and types that are not bounds-checked<\/li>\n<\/ul>\n<p>The four rules to bounds safety mention three rules of the C++ core guidelines. As in the last posts to the profiles, I will make my additions if necessary.<\/p>\n<\/p>\n<h3>Bounds.1: Don\u2019t use pointer arithmetic, <code><\/code><code class=\"highlighter-rouge no-highlight\"><\/code>Bounds.2: Only index into arrays using constant expressions and Bounds.3: No array-to-pointer decay<\/h3>\n<p>The reason for the three rules boils down to the three do&#8217;s: pass pointers to single objects (only), keep pointer arithmetic simple, and use <span style=\"font-family: courier new, courier;\">std::span.<\/span> The first do can also be formulated negatively: don&#8217;t pass pointers to arrays. I assume you don&#8217;t know std::span.<span style=\"font-family: courier new, courier;\"> std::span&lt;T&gt;<\/span> represents a non-owning range of contiguous memory. This range can be an array, a pointer with a size, or a <span style=\"font-family: courier new, courier;\">std::vector<\/span>.<\/p>\n<p>Let me cite the words of the guidelines: &#8220;<em>Complicated pointer manipulation is a major source of errors<\/em>.&#8221;. Why should we care? Of course, our legacy code is full of functionality, such as this example:<\/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>(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span> p, <span style=\"color: #007788; font-weight: bold;\">int<\/span> count)\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (count <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span> q <span style=\"color: #555555;\">=<\/span> p <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">1<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD<\/span>\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">*<\/span>p<span style=\"color: #555555;\">++<\/span>;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD<\/span>\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (count <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    p[<span style=\"color: #ff6600;\">4<\/span>] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD<\/span>\r\n\r\n    p[count <span style=\"color: #555555;\">-<\/span> <span style=\"color: #ff6600;\">1<\/span>] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD<\/span>\r\n\r\n    use(<span style=\"color: #555555;\">&amp;<\/span>p[<span style=\"color: #ff6600;\">0<\/span>], <span style=\"color: #ff6600;\">3<\/span>);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ BAD<\/span>\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> myArray[<span style=\"color: #ff6600;\">100<\/span>];     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\nf(myArray, <span style=\"color: #ff6600;\">100<\/span>),      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The main issue with&nbsp;this code is that the caller&nbsp;must&nbsp;provide the&nbsp;correct length of the&nbsp;C-array. If not, we get undefined behavior.<\/p>\n<p>Think about the last lines (1) and (2) for a few seconds. We start with an array and remove its type information by passing it to the function f. This process is called an array to pointer decay and is the reason for many errors. Maybe we had a bad day, and we counted the number of elements wrong, or the size of the C-array changed. Anyway, the result is the same: undefined behavior. The same argumentation will also hold for a C-string.<\/p>\n<p>What should we do? We should use a suitable data type. C++20 supports <span style=\"font-family: courier new, courier;\">std::span<\/span><span style=\"font-family: 'courier new', courier;\"><\/span>. Have a look here:<\/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>(span<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: #0099ff; font-style: italic;\">\/\/ BETTER: use span in the function declaration<\/span>\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.length() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> a[<span style=\"color: #ff6600;\">0<\/span>];      <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n\r\n    span<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> q <span style=\"color: #555555;\">=<\/span> a.subspan(<span style=\"color: #ff6600;\">1<\/span>); <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.length() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    a[<span style=\"color: #ff6600;\">4<\/span>] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n\r\n    a[count <span style=\"color: #555555;\">-<\/span> <span style=\"color: #ff6600;\">1<\/span>] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n\r\n    use(a.data(), <span style=\"color: #ff6600;\">3<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Fine! <span style=\"font-family: 'courier new', courier;\">std::span<\/span> checks at run-time its boundaries.<\/p>\n<p>But I hear your complaints: We don&#8217;t have C++20. No problem. It&#8217;s pretty easy to rewrite the functions <span style=\"font-family: 'courier new', courier;\">f<\/span> <span style=\"font-family: 'courier new', courier;\"><\/span>using the container&nbsp;<span style=\"font-family: 'courier new', courier;\">std:<\/span>:<span style=\"font-family: 'courier new', courier;\">array<\/span> and the method&nbsp;<span style=\"font-family: 'courier new', courier;\">std::array::at<\/span>. Here we are:<\/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;\">\/\/ spanVersusArray.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;array&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">use<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/span>){}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f<\/span>(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;\">100<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span> a){\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.size() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> a.at(<span style=\"color: #ff6600;\">0<\/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;\">99<\/span><span style=\"color: #555555;\">&gt;<\/span> q;\r\n    std<span style=\"color: #555555;\">::<\/span>copy(a.begin() <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">1<\/span>, a.end(), q.begin());      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.size() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    a.at(<span style=\"color: #ff6600;\">4<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;          \r\n\r\n    a.at(a.size() <span style=\"color: #555555;\">-<\/span> <span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n\r\n    use(a.data(), <span style=\"color: #ff6600;\">3<\/span>); \r\n}\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;\">100<\/span><span style=\"color: #555555;\">&gt;<\/span> arr{};\r\n\r\n    f(arr);\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The <span style=\"font-family: 'courier new', courier;\">std:<\/span>:<span style=\"font-family: 'courier new', courier;\">array::at<\/span> operator will check at runtime its bounds. If <span style=\"font-family: 'courier new', courier;\">pos &gt;= size()<\/span>, you will get an <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/error\/out_of_range\"><span style=\"font-family: 'courier new', courier;\">std::out_of_range<\/span><\/a><span style=\"color: #000000; font-family: DejaVuSans, 'DejaVu Sans', arial, sans-serif; font-size: 12.8px; 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; float: none;\">&nbsp;exception. <\/span><span style=\"color: #000000; font-family: DejaVuSans, 'DejaVu Sans', arial, sans-serif; font-size: 12.8px; 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; float: none;\">Looking carefully at the <span style=\"font-family: 'courier new', courier;\">spanVersusArray.cpp<\/span>&nbsp;program, you will notice two issues. First, the expression (1) is more verbose than the<span style=\"font-family: courier new, courier;\"> std::span<\/span> version, and second, the size of the <span style=\"font-family: 'courier new', courier;\">std:<\/span><\/span><span style=\"font-family: courier new, courier;\">:array<\/span> <span style=\"color: #000000; font-family: DejaVuSans, 'DejaVu Sans', arial, sans-serif; font-size: 12.8px; 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; float: none;\">is part of the signature of the function&nbsp;<span style=\"font-family: 'courier new', courier;\">f.<\/span> This isn&#8217;t good. I can only use f with the type <span style=\"font-family: 'courier new', courier;\">std::array<\/span><\/span><span style=\"color: #000000; font-family: DejaVuSans, 'DejaVu Sans', arial, sans-serif; font-size: 12.8px; 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; float: none;\"><span style=\"font-family: 'courier new', courier;\">&lt;int, 100&gt;<\/span>.&nbsp; In this case, the checks of the array size inside the function are superfluous.&nbsp;<\/span><\/p>\n<p>To your rescue, C++ has templates; therefore, overcoming the type restrictions is easy but staying type-safe.<\/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;\">\/\/ at.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;array&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;deque&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&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: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> use(T<span style=\"color: #555555;\">*<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/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: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> f(T<span style=\"color: #555555;\">&amp;<\/span> a){\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.size() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">2<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> a.at(<span style=\"color: #ff6600;\">0<\/span>);      \r\n\r\n    std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">::<\/span>value_type , <span style=\"color: #ff6600;\">99<\/span><span style=\"color: #555555;\">&gt;<\/span> q;                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>copy(a.begin() <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">1<\/span>, a.end(), q.begin());     \r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (a.size() <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">6<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n\r\n    a.at(<span style=\"color: #ff6600;\">4<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;          \r\n\r\n    a.at(a.size() <span style=\"color: #555555;\">-<\/span> <span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n\r\n    use(a.data(), <span style=\"color: #ff6600;\">3<\/span>);                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\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;\">100<\/span><span style=\"color: #555555;\">&gt;<\/span> arr{};                                             \r\n    f(arr);                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/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;\">double<\/span>, <span style=\"color: #ff6600;\">20<\/span><span style=\"color: #555555;\">&gt;<\/span> arr2{};\r\n    f(arr2);                                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/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;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> vec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>};\r\n    f(vec);                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>string myString<span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"123456789\"<\/span>;\r\n    f(myString);                                               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ std::deque&lt;int&gt; deq{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ f(deq);                                                 <\/span>\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, the function f works for <span style=\"font-family: 'courier new', courier;\">std:<\/span>:<span style=\"font-family: courier new, courier;\">array<\/span>&#8216;s of different sizes and types (lines (1) and (2)) but also for a <span style=\"font-family: 'courier new', courier;\">std:<\/span>:<span style=\"font-family: courier new, courier;\">vector&nbsp;<\/span>(3) or a&nbsp;<span style=\"font-family: 'courier new', courier;\">std:<\/span><span style=\"font-family: courier new, courier;\">:string&nbsp;<\/span>(4). These containers have in common that their data is stored in a&nbsp;contiguous memory block. This will no hold <span style=\"font-family: 'courier new', courier;\">std::deque;<\/span> therefore, the call <span style=\"font-family: 'courier new', courier;\">a.data()<\/span> in expression (6) fails. A <span style=\"font-family: courier new, courier;\">std::deque<\/span> is a kind of doubly-linked list of small memory blocks.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5380\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque.png\" alt=\"deque\" width=\"500\" height=\"38\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque.png 2174w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque-300x22.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque-1024x76.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque-768x57.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque-1536x114.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/deque-2048x152.png 2048w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The expression&nbsp;<span style=\"font-family: 'courier new', courier;\">T::value_type<\/span> (5) helps me get each container&#8217;s underlying value type. <span style=\"font-family: 'courier new', courier;\">T<\/span> is a so-called dependent type because <span style=\"font-family: 'courier new', courier;\">T<\/span> is a type parameter of the function template <span style=\"font-family: 'courier new', courier;\">f<\/span>. This is the reason I have to give the compiler a hint that <span style=\"font-family: 'courier new', courier;\">T::value_type<\/span> is a type:&nbsp;<span style=\"font-family: 'courier new', courier;\">typename T::value_type<\/span>.<\/p>\n<h3>Bounds.4: Don\u2019t use standard-library functions and types that are not bounds-checked<\/h3>\n<p>I have already written a post <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-avoid-bound-errors\">C++ Core Guidelines: Avoid Bounds Errors<\/a>. This post gives background information to this rule and provides do&#8217;s.&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The name of the third profile is Lifetime Safety Profile. This profile which is the topic of my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-lifetime-safety\">next post<\/a>, boils down to one rule: Don\u2019t dereference a possibly invalid pointer.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s post concerns the second C++ Core Guidelines: Bounds Safety profile. The goal of the profile bounds safety is that you operate inside the bounds of allocated memory.<\/p>\n","protected":false},"author":21,"featured_media":5780,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[466],"class_list":["post-5781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-safety"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5781","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=5781"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5781\/revisions"}],"predecessor-version":[{"id":6772,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5781\/revisions\/6772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5780"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}