{"id":4863,"date":"2014-03-30T16:10:09","date_gmt":"2014-03-30T16:10:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/freeing-all-malloc-created-pointers-with-one-command-collection-of-common-programming-errors\/"},"modified":"2014-03-30T16:10:09","modified_gmt":"2014-03-30T16:10:09","slug":"freeing-all-malloc-created-pointers-with-one-command-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/freeing-all-malloc-created-pointers-with-one-command-collection-of-common-programming-errors\/","title":{"rendered":"Freeing all malloc()-created pointers with one command?-Collection of common programming errors"},"content":{"rendered":"<p>No, you can&#8217;t. There are various ways to sort-of accomplish this, but some of them encourage sloppy habits (for example, most runtimes on most OSs will release all the memory you allocated when your process exists, but this won&#8217;t help you during the execution of your program and usually won&#8217;t release shared resources that are not core memory).<\/p>\n<p>One viable method is to use a pool allocator &#8212; there are plenty of resources on the topic if you ask Google about it. The concept is rather simple: you allocate (with <code>malloc<\/code>) one large pool of memory yourself at some start time, and then you manually suballocate (with your own hand-rolled function) from that pool, just returning pointers to blocks of the appropriate size that you keep track of yourself. Then you can free everything with one call to <code>free<\/code> that matches the original <code>malloc<\/code> call.<\/p>\n<p>However, the reason to do this isn&#8217;t so that you can be lazy with your memory handling: you do this because of some performance-related reasons, usually, like providing superior locality of reference of small allocations or some such. If you want to do this just because you don&#8217;t want to think about remembering to release the resources you allocate, well&#8230; that&#8217;s not so great.<\/p>\n<p>Other languages provide mechanisms (such as built-in garbage collectors, like C#, or RAII, like C++). But in C you generally need to be aware of your memory usage and manage it yourself, because there are fewer mechanisms to automate the cleanup for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No, you can&#8217;t. There are various ways to sort-of accomplish this, but some of them encourage sloppy habits (for example, most runtimes on most OSs will release all the memory you allocated when your process exists, but this won&#8217;t help you during the execution of your program and usually won&#8217;t release shared resources that are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4863","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4863"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4863\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}