{"id":3831,"date":"2019-11-09T00:01:39","date_gmt":"2019-11-09T08:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3831"},"modified":"2019-11-16T08:43:13","modified_gmt":"2019-11-16T16:43:13","slug":"playing-with-memory","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3831","title":{"rendered":"Playing with Memory"},"content":{"rendered":"<p>Gone are the old days when your C program ruled the entire computer&#8217;s domain. Back then, you could access any chunk of memory in the computer, manipulate it in all sorts of interesting ways, and not be concerned that your code&#8217;s actions would be restricted. Ah, those were good times.<br \/>\n<!--more--><br \/>\nOf course, such freedom meant your program could tear down the entire system. This danger didn&#8217;t seem to be a concern back in the day. Still, within its allocated memory chunk, your C code today can manipulate memory in all sorts of interesting ways. The key function is <em>memcpy()<\/em>:<\/p>\n<p><code>void * memcpy(void *restrict dst, const void *restrict src, size_t n);<\/code><\/p>\n<p>Defined in the <code>string.h<\/code> header file, the <em>memcpy()<\/em> or &#8220;mem-copy&#8221; function copies <code>n<\/code> bytes of storage from memory location <code>src<\/code> to memory location <code>dst<\/code>. Both locations must be allocated in your code; you can&#8217;t specify direct storage addresses.<\/p>\n<p>One common place I&#8217;ve seen <em>memcpy()<\/em> used is to duplicate a structure:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    struct person {\r\n        int age;\r\n        char name[12];\r\n    } man = { 34, \"George\" };\r\n    struct person *guy;\r\n\r\n    <span class=\"comments\">\/* output the structure *\/<\/span>\r\n    printf(\"%s is %d years old\\n\",man.name,man.age);\r\n\r\n    <span class=\"comments\">\/* allocate the buffer *\/<\/span>\r\n    guy = (struct person *)malloc( sizeof(struct person) );\r\n    if( guy==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* copy memory *\/<\/span>\r\n    memcpy(guy,&amp;man,sizeof(struct person));\r\n\r\n    <span class=\"comments\">\/* output the duplicate structure *\/<\/span>\r\n    printf(\"%s is %d years old\\n\",guy-&gt;name,guy-&gt;age);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>struct person<\/em> variable <code>man<\/code> is defined and declared right away. Its member&#8217;s values are output.<\/p>\n<p>The <em>malloc()<\/em> function allocates a chunk of memory, <code>guy<\/code>, then <em>memcpy()<\/em> duplicates the contents of the <code>man<\/code> variable into the newly-allocated <code>guy<\/code> chunk.<\/p>\n<p>Finally, the <code>guy<\/code> buffer&#8217;s output is displayed, which is identical to the original:<\/p>\n<p><code>George is 34 years old<br \/>\nGeorge is 34 years old<\/code><\/p>\n<p>Of course, all this action is a bit much. You could just duplicate the structure from one variable to another, as this variation on the code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct person {\r\n        int age;\r\n        char name[12];\r\n    } man = { 34, \"George\" };\r\n    struct person guy;\r\n\r\n    <span class=\"comments\">\/* output the structure *\/<\/span>\r\n    printf(\"%s is %d years old\\n\",man.name,man.age);\r\n\r\n    <span class=\"comments\">\/* copy structure *\/<\/span>\r\n    guy = man;\r\n\r\n    <span class=\"comments\">\/* output the duplicate structure *\/<\/span>\r\n    printf(\"%s is %d years old\\n\",guy.name,guy.age);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This version is shorter, and it does the same thing: <code>guy = man<\/code> takes care of everything.<\/p>\n<p>The true advantage of <em>memcpy()<\/em> is that it can duplicate the contents of memory into a <em>void<\/em> buffer. Once there, you can examine it as raw data or however you please. I cover this process in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3833\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>memcpy<\/em> function allows chunks of memory to be copied hither and thither, which can lead to some interesting results. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3831\">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-3831","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\/3831","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=3831"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3831\/revisions"}],"predecessor-version":[{"id":3861,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3831\/revisions\/3861"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}