{"id":5982,"date":"2023-08-26T00:01:05","date_gmt":"2023-08-26T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5982"},"modified":"2023-09-02T11:02:12","modified_gmt":"2023-09-02T18:02:12","slug":"memory-files","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5982","title":{"rendered":"Memory Files"},"content":{"rendered":"<p>In the Unix environment, everything is a file. For example, you can open the terminal as a file, which I covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4385\">a previous Lesson<\/a>. But what about memory? Can you open memory as a file? And why would you want to?<br \/>\n<!--more--><br \/>\nThe traditional paradigm is to allocate memory. Operators and functions manipulate memory, but you don&#8217;t read from and write to memory as you would a file. I think it would be convenient to do so. If it&#8217;s not convenient, then it&#8217;s a fun exercise to try, which can also help you understand a bit more about how files work in the computer.<\/p>\n<p>As with writing to a file, you need a handle to write to a memory-file. For a file, the <em>FILE<\/em> handle is a pointer to a structure that contains file information. A memory-file handle works similarly, containing details about the memory chunk pretending to be a file. It&#8217;s not long-term storage, nor would the memory-file be available to other programs unless the memory handle were implemented at the operating system level.<\/p>\n<p>Oh, but I prattle.<\/p>\n<p>Here is my notion for a memory handle structure used to manage a chunk of memory like a file:<\/p>\n<pre class=\"screen\">\r\nstruct mem_file {\r\n\tvoid *address;\r\n\tchar *name;\r\n\tint size;\r\n\tint offset;\r\n\tchar *timestamp;\r\n};<\/pre>\n<p>Most important is the memory chunk&#8217;s address, pointer member <code>address<\/code>. Then comes a <code>name<\/code> for the memory chunk.<\/p>\n<p>The memory chunk&#8217;s size is contained in member <code>size<\/code>. This value represents the number of bytes stored in the memory-file, not its allocated memory size.<\/p>\n<p>The <code>offset<\/code> member is used when reading data from the memory-file. It works like the file position indicator in the <em>fseek()<\/em> function.<\/p>\n<p>Finally, a <code>timestamp<\/code> string is stored in the <em>mem_file<\/em> structure.<\/p>\n<p>As with file storage, a constant defines this size of memory-file allocated chunks:<\/p>\n<p><code>#define DEFAULT_SIZE 2048<\/code><\/p>\n<p>When considering this value, understand that different file systems use different allocation sizes. For example, a file system may automatically allocate 4K of storage (4096 bytes) when the file is created. In the olden days, DOS allocated file storage in 512 byte chunks, which was the size of a floppy disk sector. (Sectors are a subdivision of a track on the floppy disk, like slices in a pie.)<\/p>\n<p>I chose the value 2048 (2K) as a typical memory chunk. More storage is allocated if necessary as the memory-file grows. The <code>size<\/code> member of the <em>mem_file<\/em> structure is monitored and when it crosses the <code>DEFAULT_SIZE<\/code> value, more memory is allocated for the memory-file. This operation is done internally, just as it would be for a file stored on mass media.<\/p>\n<p>To use the <em>mem-file<\/em> handle, a memory-file must be opened or created. Unlike a media file, memory files are opened for reading and writing simultaneously. And like a media file, memory files can be closed, which frees the memory.<\/p>\n<p>Unlike media files, a memory-file doesn&#8217;t remain in storage after it&#8217;s closed. I could code something like that, but it requires more overhead in the form a memory-file file system, similar to the old &#8220;mem drive&#8221; utility in DOS. For now, I just want to explore the memory-file concept on a simple level.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6013\">next week&#8217;s Lesson<\/a>, I present the <em>mem_open()<\/em> and <em>mem_close()<\/em> functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What if memory could be opened and accessed like a file? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5982\">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-5982","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\/5982","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=5982"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5982\/revisions"}],"predecessor-version":[{"id":6032,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5982\/revisions\/6032"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}