{"id":5315,"date":"2022-04-23T00:01:41","date_gmt":"2022-04-23T07:01:41","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5315"},"modified":"2022-04-16T09:42:49","modified_gmt":"2022-04-16T16:42:49","slug":"desperately-freeing-allocated-memory","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5315","title":{"rendered":"Desperately Freeing Allocated Memory"},"content":{"rendered":"<p>The critical issue about allocating and then freeing memory is to avoid a memory leak. This condition happens when a memory chunk gets lost, leaving the it lingering in RAM not doing anyone any good. Most often a memory leak occurs in a function.<br \/>\n<!--more--><br \/>\nOf course, if the function allocates memory and returns that chunk for some useful purpose &mdash; don&#8217;t free it! Otherwise, for a temporary buffer, free that memory before the function exits. When you don&#8217;t, memory overflows, the system becomes sluggish, the program crashes, and peril portends the planet. These are unwanted things.<\/p>\n<p>The following code features the <em>memory()<\/em> function, which allocates 1GB chunks and leaves them lingering.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_04_23-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_04_23-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nvoid memory(void)\r\n{\r\n    char *p;\r\n\r\n    p = malloc(1024*1024*1024);        <span class=\"comments\">\/* 1GB *\/<\/span>\r\n    if( p==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for( x=0; x&lt;1024; x++ )\r\n    {\r\n        printf(\"%4d - Allocating memory: \",x);\r\n        memory();\r\n        printf(\"done!\\n\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop in the <em>main()<\/em> function repeats at least 1,024 times, calling the <em>memory()<\/em> function over and over. Each time, the <em>memory()<\/em> function allocates 1GB of RAM and leaves it in memory: lost, unavailable, and unrecoverable.<\/p>\n<p>Theoretically, the program allocates and wastes 1TB of memory if it runs to its conclusion. This memory is recovered when the program quits, of course, but during its run the memory is wasted.<\/p>\n<p>On my Macintosh and under Linux, the program runs to completion. Weird. My Mac has 32GB of RAM installed, not 1TB. So something hinky is going on.<\/p>\n<p>In Windows, the <em>for<\/em> loop runs 117 times before the <em>malloc()<\/em> function fails and the program quits (releasing the memory).<\/p>\n<p>This example is extreme, but it works to illustrate how you&#8217;re supposed to free memory: In a function, when memory is allocated and no longer needed, use the <em>free()<\/em> function. (<a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_04_23-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to see the corrected version of the code.)<\/p>\n<p>In the <em>main()<\/em> function, yes you can <em>free()<\/em> allocated memory before that final <em>return<\/em> statement. These statements can appear in any order; you need not free memory in the same sequence (or reverse sequence) by which it was allocated. Freeing memory makes your code pleasing to the pedants who demand such things, but the absence of a parade of <em>free()<\/em> statements at the end of the <em>main()<\/em> function isn&#8217;t deleterious to the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most definitely in a function, if the allocated memory is no longer needed, free it! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5315\">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-5315","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\/5315","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=5315"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5315\/revisions"}],"predecessor-version":[{"id":5328,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5315\/revisions\/5328"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}