{"id":5681,"date":"2019-05-16T17:09:38","date_gmt":"2019-05-16T17:09:38","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-standard-library\/"},"modified":"2019-05-16T17:09:38","modified_gmt":"2019-05-16T17:09:38","slug":"c-core-guidelines-the-standard-library","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-standard-library\/","title":{"rendered":"C++ Core Guidelines: The Standard Library"},"content":{"rendered":"<p>The rules to the C++ standard library are mainly about containers, strings, and iostreams.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5679\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/iso-154533_1280.png\" alt=\"iso 154533 1280\" width=\"400\" height=\"396\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/iso-154533_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/iso-154533_1280-300x297.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/iso-154533_1280-1024x1014.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/iso-154533_1280-150x150.png 150w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/iso-154533_1280-768x761.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Curiously, there is no section on the standard template library (STL) algorithms in this chapter. Curiously, because there is a proverb in the C++ community: If you write an explicit loop, you don&#8217;t know the algorithms of the STL. Anyway. Only for completeness, let me start with the first three rules which do provide not much beef.<\/p>\n<p><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rsl-lib\">SL.1: Use libraries wherever possible<\/a>, because reinventing the wheel is a bad idea. Additionally, you benefit from the work of others. This means you use already tested and well-defined functionality. This holds, in particular, true if you <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rsl-sl\">SL.2: Prefer the standard library to other libraries.&nbsp;<\/a>Imagine, for example, you hire someone. The benefit is that he already knows the library, and you don&#8217;t have to teach him your libraries. You save a lot of money and time. I once had a customer who named his infrastructure namespace <span style=\"font-family: courier new, courier;\">std<\/span>. Of course, if you want to have a lot of fun, do it. If not: <a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#sl-std\">SL.3: Do not add non-standard entities to namespace <code class=\"highlighter-rouge no-highlight\">std<\/code><\/a>.<\/p>\n<p>The next rules to STL containers are more concrete.<\/p>\n<\/p>\n<h2>Containers<\/h2>\n<p>&nbsp;The first rule is quite easy to argue.<\/p>\n<h3><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rsl-arrays\">SL.con.1: Prefer using STL <code class=\"highlighter-rouge no-highlight\">array<\/code> or <code class=\"highlighter-rouge no-highlight\">vector<\/code> instead of a C array<\/a><\/h3>\n<p>I assume you know a <span style=\"font-family: courier new, courier;\">std::vector. <\/span>One of the significant advantages of a <span style=\"font-family: courier new, courier;\">std::vector<\/span> to a C array is that the <span style=\"font-family: courier new, courier;\">std::vector<\/span> automatically manages its memory. Of course, that holds true for all further containers of the Standard Template Library. But now, let&#8217;s look closer at the automatic memory management of <span style=\"font-family: courier new, courier;\">std::vector<\/span>.<span style=\"font-family: courier new, courier;\"><\/span><\/p>\n<h3><span style=\"font-family: courier new, courier;\">std::vector<\/span><\/h3>\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;\">\/\/ vectorMemory.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&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> showInfo(<span style=\"color: #006699; font-weight: bold;\">const<\/span> T<span style=\"color: #555555;\">&amp;<\/span> t,<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> name){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> name <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" t.size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> t.size() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> name <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" t.capacity(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> t.capacity() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\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>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>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> vec;                                            <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> <span style=\"color: #cc3300;\">\"Maximal size: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"vec.max_size(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> vec.max_size() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  <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> 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;\">\"Empty vector: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    showInfo(vec, <span style=\"color: #cc3300;\">\"Vector\"<\/span>);\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>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Initialised with five values: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;   \r\n    vec <span style=\"color: #555555;\">=<\/span> {<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>};\r\n    showInfo(vec, <span style=\"color: #cc3300;\">\"Vector\"<\/span>);                                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\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>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Added four additional values: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    vec.insert(vec.end(),{<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    showInfo(vec,<span style=\"color: #cc3300;\">\"Vector\"<\/span>);                                         <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> 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;\">\"Resized to 30 values: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    vec.resize(<span style=\"color: #ff6600;\">30<\/span>);\r\n    showInfo(vec,<span style=\"color: #cc3300;\">\"Vector\"<\/span>);                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\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>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Reserved space for at least 1000 values: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    vec.reserve(<span style=\"color: #ff6600;\">1000<\/span>);\r\n    showInfo(vec,<span style=\"color: #cc3300;\">\"Vector\"<\/span>);                                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\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>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Shrinke to the current size: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    vec.shrink_to_fit();                                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (7)<\/span>\r\n    showInfo(vec,<span style=\"color: #cc3300;\">\"Vector\"<\/span>);\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>To spare typing, I wrote the small function <span style=\"font-family: courier new, courier;\">showInfo<\/span>. This function returns for a vector its size and its capacity. The size of a vector is its number of elements, and the capacity of a container is the number of elements a vector can hold without an additional memory allocation. Therefore, the capacity of a vector has at least to be as big as its size. You can adjust the size of a vector with its method <span style=\"font-family: courier new, courier;\">resize<\/span> and the capacity of a container with its method <span style=\"font-family: courier new, courier;\">reserve<\/span>.<\/p>\n<p style=\"text-align: left;\">But, back to the program from top to bottom. I create (line 1) an empty vector. Afterward, the program displays (line 2) the maximum number of elements a vector can have. After each operation, I return their size and capacity. That holds for the initialization of the vector (line 3), for the addition of four new elements (line 4), the resizing of the containers to 30 elements (line 5), and the reserving of additional memory for at least 1000 elements (line 6). With C++11, you can shrink with the method <span style=\"font-family: courier new, courier;\">shrink_to_fit<\/span> (line 7) the vector&#8217;s capacity to its size.<\/p>\n<p>Before I present the program&#8217;s output on Linux, let me make a few remarks.<\/p>\n<ol>\n<li>Adjusting the size and capacity of the container is done automatically. I haven&#8217;t used any memory operations like <span style=\"font-family: courier new, courier;\">new<\/span> and <span style=\"font-family: courier new, courier;\">dele<\/span><\/li>\n<li>By using the method vec<span style=\"font-family: courier new, courier;\">.resize(n)<\/span> the vector vec<span style=\"font-family: courier new, courier;\"> <\/span>will get new default-initialized elements if<span style=\"font-family: courier new, courier;\"> <\/span><span style=\"font-family: courier new, courier;\">n &gt; cont.size() <\/span>holds.<\/li>\n<li>By using the method vec<span style=\"font-family: courier new, courier;\">.reserve(n)<\/span> the container <span style=\"font-family: courier new, courier;\">vec<\/span> will get new memory for at least n elements if<span style=\"font-family: courier new, courier;\"> <\/span><span style=\"font-family: courier new, courier;\">n &gt; cont.capacity()<\/span> holds.<\/li>\n<li>The call <span style=\"font-family: courier new, courier;\">shrink_to_fit<\/span> is non-binding. That means the C++ runtime has not had to adjust the capacity of a container to its size. But, using the method <span style=\"font-family: courier new, courier;\">shrink_to_fit<\/span> with GCC, clang, or cl.exe always freed the unnecessary memory.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5680\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/vectorMemory.PNG\" alt=\"vectorMemory\" width=\"400\" height=\"576\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/vectorMemory.PNG 1118w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/vectorMemory-208x300.png 208w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/vectorMemory-711x1024.png 711w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/vectorMemory-768x1107.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/vectorMemory-1066x1536.png 1066w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Okay, but what is the difference between a C array and a C++ array?<\/p>\n<h3>std::array<\/h3>\n<p><span style=\"font-family: courier new, courier;\">std::array<\/span> combines the best of two worlds. On the one hand, <span style=\"font-family: courier new, courier;\">std::array<\/span> has the size and efficiency of a C-array; on the other hand, <span style=\"font-family: courier new, courier;\">std::array<\/span> has the interface of a <span style=\"font-family: courier new, courier;\">std::vector.<\/span>&nbsp;<\/p>\n<p>My small program compares the memory efficiency of a C array, a C++ array (<span style=\"font-family: courier new, courier;\">std:<\/span>:array), and a <span style=\"font-family: courier new, courier;\">std::vector.<\/span>&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;\">\/\/ sizeof.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;array&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&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>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(int)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>) <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> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> cArr[<span style=\"color: #ff6600;\">10<\/span>] <span style=\"color: #555555;\">=<\/span> {<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>, <span style=\"color: #ff6600;\">10<\/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;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span> cppArr <span style=\"color: #555555;\">=<\/span> {<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>, <span style=\"color: #ff6600;\">10<\/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> cppVec <span style=\"color: #555555;\">=<\/span> {<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>, <span style=\"color: #ff6600;\">10<\/span>};\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(cArr)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(cArr) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;            <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> <span style=\"color: #cc3300;\">\"sizeof(cppArr)= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(cppArr) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  \r\n                                                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(cppVec) = \"<\/span>   <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(cppVec) <span style=\"color: #555555;\">+<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>) <span style=\"color: #555555;\">*<\/span> cppVec.capacity() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"               = sizeof(cppVec): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(cppVec) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"               + sizeof(int)* cppVec.capacity(): \"<\/span>   <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>)<span style=\"color: #555555;\">*<\/span> cppVec.capacity() <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> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5081\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/sizeof.png\" alt=\"sizeof\" width=\"500\" height=\"263\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>The C array (line 1) and the C++ array (line 2) take 40 bytes. That is precisely <span style=\"font-family: courier new, courier;\">sizeof(int) * 10.<\/span> In contrast, the <span style=\"font-family: courier new, courier;\">std::vector<\/span> needs additional 24 bytes (line 3) to manage its data on the heap.<span style=\"font-family: courier new, courier;\"> <\/span><\/p>\n<p>This was the C part of a std::array, but the std::array supports the interface of a std::vector. This means, in particular, that <span style=\"font-family: courier new, courier;\">std:<\/span>:array knows its size; therefore, error-prone interfaces such as the following one are a heavy code smell.<\/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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">bad<\/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}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> myArray[<span style=\"color: #ff6600;\">100<\/span>] <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #ff6600;\">0<\/span>};    \r\nbad(myArray, <span style=\"color: #ff6600;\">100<\/span>);\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ ----------------------------- <\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">good<\/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;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span> arr){\r\n   ...\r\n}\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;\">100<\/span><span style=\"color: #555555;\">&gt;<\/span> myArray <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #ff6600;\">0<\/span>};\r\ngood(myArray);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you use a C array as a function argument, you remove almost all type information and pass it as a pointer to its first argument. This is extremely error-prone because you must also provide the number of elements. If your function accepts a <span style=\"font-family: courier new, courier;\">std::array&lt;int, 100&gt;<\/span>, this will not hold.<\/p>\n<p>You can use a template if the function good is not generic enough.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">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> foo(T<span style=\"color: #555555;\">&amp;<\/span> arr){\r\n\r\n   arr.size();                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n}\r\n\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;\">100<\/span><span style=\"color: #555555;\">&gt;<\/span> arr{};                                             \r\nfoo(arr);                                                    \r\n    \r\nstd<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\nfoo(arr2);  \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Because a <span style=\"font-family: courier new, courier;\">std::array<\/span> knows its size, you can ask for it in line 1.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The following two rules for containers are pretty interesting. In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-standard-library\">next post<\/a>, I will give an answer to the question: When to use which container?<\/p>\n<p>&nbsp;<\/p>\n<div id=\"simple-translate\">&nbsp;<\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The rules to the C++ standard library are mainly about containers, strings, and iostreams.<\/p>\n","protected":false},"author":21,"featured_media":5679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5681","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=5681"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5681\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5679"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}