{"id":108591,"date":"2023-08-11T07:00:00","date_gmt":"2023-08-11T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108591"},"modified":"2023-08-11T06:40:19","modified_gmt":"2023-08-11T13:40:19","slug":"20230811-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230811-00\/?p=108591","title":{"rendered":"Inside STL: The array"},"content":{"rendered":"<p>The C++ standard library <code>array<\/code> is just a C-style array wrapped inside a class so that it behaves like a normal C++ object instead of a wacky thing that undergoes decay.<\/p>\n<pre>template&lt;typename T, size_t N&gt;\r\nclass array\r\n{\r\n    T elements[N];\r\n};\r\n\r\ntemplate&lt;typename T&gt;\r\nclass array&lt;T, 0&gt;\r\n{\r\n};\r\n<\/pre>\n<p>The only weird case is <var>N<\/var>\u00a0= 0. You are allowed to create a zero-length <code>std::array<\/code>, but C++ does not allow zero-length C-style arrays. The zero-length <code>std::array<\/code> is just an empty class.<\/p>\n<p>Visual Studio and the Windows debugger come with a visualizer:<\/p>\n<pre>0:000&gt; dx a\r\na                : { size=5 } [Type: std::array&lt;int,5&gt;]\r\n    [&lt;Raw View&gt;]     [Type: std::array&lt;int,5&gt;]\r\n    [0]              : 3 [Type: int]\r\n    [1]              : 1 [Type: int]\r\n    [2]              : 4 [Type: int]\r\n    [3]              : 1 [Type: int]\r\n    [4]              : 5 [Type: int]\r\n<\/pre>\n<p>Or you can just dig out the elements yourself.<\/p>\n<pre>0:000&gt; ?? a\r\nclass std::array&lt;int,5&gt;\r\n   +0x000 _Elems           : [5] 0n3\r\n0:000&gt; ?? a._Elems[1]\r\nint 0n1\r\n<\/pre>\n<p>The <code>dt<\/code> command with the <code>-a<\/code> option (array) is handy for dumping C-style arrays.<\/p>\n<pre>0:000&gt; ?? &amp;a\r\nclass std::array&lt;int,5&gt; * 0x00000094`bed0f698\r\n   +0x000 _Elems           : [5] 0n3\r\n0:000&gt; dt -a5 int 0x00000094`bed0f698\r\n[0] @ 00000094`bed0f698\r\n---------------------------------------------\r\n0n3\r\n\r\n[1] @ 00000094`bed0f69c\r\n---------------------------------------------\r\n0n1\r\n\r\n[2] @ 00000094`bed0f6a0\r\n---------------------------------------------\r\n0n4\r\n\r\n[3] @ 00000094`bed0f6a4\r\n---------------------------------------------\r\n0n1\r\n\r\n[4] @ 00000094`bed0f6a8\r\n---------------------------------------------\r\n0n5\r\n<\/pre>\n<p>The simplicity of the <code>std::array<\/code> is a nice break from the complexity of <code>std::deque<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s just an array.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-108591","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s just an array.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=108591"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108591\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=108591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}