{"id":3086,"date":"2018-05-12T00:01:56","date_gmt":"2018-05-12T07:01:56","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3086"},"modified":"2018-05-05T14:03:13","modified_gmt":"2018-05-05T21:03:13","slug":"marching-through-an-array-in-memory","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3086","title":{"rendered":"Marching Through an Array in Memory"},"content":{"rendered":"<p>C language pointers are smarter than the old BASIC peek and poke commands. A pointer knows the the type of value it&#8217;s referencing in memory, specifically how large the value is or how many bytes of storage it occupies. You can use the Code::Blocks debugger to witness such pointer intelligence.<br \/>\n<!--more--><br \/>\nStart a new Console project in Code::Blocks, for the C language, and name it <strong>Integer Array<\/strong>. Ensure that you create a Debug configuration for the project. Set this code for <code>main.c<\/code>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    int array[5],x,*ptr;\r\n\r\n    for(x=0;x&lt;5;x++)\r\n        array[x] = x*2;\r\n\r\n    ptr = array;\r\n    for(x=0;x&lt;5;x++)\r\n        ptr++;\r\n\r\n    return(0);\r\n}<\/pre>\n<p>An <em>int<\/em> <code>array[]<\/code> is created and filled with values. Pointer <code>ptr<\/code> is assigned to <code>array[]<\/code>, then incremented through the array. Nothing is output &mdash; yet.<\/p>\n<p>Compile the code; you don&#8217;t need to run it as it lacks output. Instead, place a stop at Line 12, the second <em>for<\/em> statement. Refer to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3075\">last week&#8217;s Lesson<\/a> for details on setting a stop and stepping through code.<\/p>\n<p>Click the Debug\/Continue button. The code runs to Line 12.<\/p>\n<p>Click the Debugging Windows button and choose Watches. In the Watches window, expand the <code>array<\/code> variable to view its five values, as shown in Figure 1. You also see the value of <code>x<\/code>: 5 after the previous <em>for<\/em> loop. The value of <code>ptr<\/code> is set to the location of <code>array<\/code> in memory.<\/p>\n<div id=\"attachment_3088\" style=\"width: 352px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3088\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure1_watches.png\" alt=\"\" width=\"342\" height=\"253\" class=\"size-full wp-image-3088\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure1_watches.png 342w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure1_watches-300x222.png 300w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><p id=\"caption-attachment-3088\" class=\"wp-caption-text\">Figure 1. Values of variables in the code, as shown in the Debugger&#8217;s Watches window.<\/p><\/div>\n<p>To view the array as stored in memory, click the Watches button and choose Memory Dump. In the Memory window&#8217;s Address box, type <strong>ptr<\/strong>, the name of the pointer variable that references <code>array[]<\/code>&#8216;s location in memory. The specific address varies, but you should see something similar to what&#8217;s shown in Figure 2.<\/p>\n<div id=\"attachment_3089\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3089\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure2_memory-dump.png\" alt=\"\" width=\"550\" height=\"228\" class=\"size-full wp-image-3089\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure2_memory-dump.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure2_memory-dump-300x124.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure2_memory-dump-500x207.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-3089\" class=\"wp-caption-text\">Figure 2. The array stored in memory.<\/p><\/div>\n<p>On my computer, <em>int<\/em> values are stored as a four-byte sequence. Each element of the array in memory is outlined in red in Figure 2. Values are stored with the least significant byte first, so you see 0, 2, 4, 6, and 8, but also a lot of zeros to pad out the full four-bytes of memory.<\/p>\n<p>With both Watches and Memory windows open, click the Step Into button on the Debugger toolbar, illustrated in Figure 3.<\/p>\n<div id=\"attachment_3090\" style=\"width: 110px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3090\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/05\/0512-figure3_step-into.png\" alt=\"\" width=\"100\" height=\"91\" class=\"size-full wp-image-3090\" \/><p id=\"caption-attachment-3090\" class=\"wp-caption-text\">Figure 3. The Step Into button.<\/p><\/div>\n<p>You see the value of <code>x<\/code> change to 0, as directed in the <em>for<\/em> loop. The value of <code>ptr<\/code> is unchanged, but click the Step Into button again.<\/p>\n<p>On my computer, the <code>ptr<\/code> value changed from 0x60fef4 to 0x60fef8, an increase of 4 bytes, which references the second array element in memory. Indeed, if you check the Memory window, you see that <code>ptr<\/code>&#8216;s memory dump is updated and the first value shown is 02 00 00 00.<\/p>\n<p>As you keep clicking the Step Into button, you see both variables <code>x<\/code> and <code>ptr<\/code> increment, as well as the results in the Memory window shift over 4 bytes. The process you&#8217;re witnessing is how an <em>int<\/em> pointer steps through an array stored in memory.<\/p>\n<p>Because the <code>sizeof(int)<\/code> on my system is 4 bytes, an <em>int<\/em> pointer knows to skip through memory four bytes at a time. In fact, if I were to perform the math <code>ptr+10<\/code>, the memory location stored in <code>ptr<\/code> is increased by 40 bytes. That&#8217;s how pointer math works. And having a debugging window show you what&#8217;s going on in memory really helps to understand the process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visually witness a pointer hopping through an array and glean an understanding of pointer math. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3086\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3086","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3086"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3086\/revisions\/3098"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}