{"id":4566,"date":"2014-03-30T13:32:43","date_gmt":"2014-03-30T13:32:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/proper-use-of-malloc-closed-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:32:43","modified_gmt":"2014-03-30T13:32:43","slug":"proper-use-of-malloc-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/proper-use-of-malloc-closed-collection-of-common-programming-errors\/","title":{"rendered":"Proper use of malloc [closed]-Collection of common programming errors"},"content":{"rendered":"<p>I think the answers are missing an important point. The size of memory is a relatively specific technical detail which isn&#8217;t of primary interest. The crucial difference is that between <em>automatic<\/em> and <em>dynamic<\/em> storage, and the associated lifetime:<\/p>\n<ul>\n<li>\n<p>Automatic storage ends at the end of the scope.<\/p>\n<\/li>\n<li>\n<p>Dynamic storage begins with <code>malloc()<\/code> and ends with <code>free()<\/code>, entirely at the discretion (and responsibility) of the user.<\/p>\n<\/li>\n<\/ul>\n<p>If you can and if it makes sense, everything should be automatic. This entails locality and well-defined interfaces. However, in C (not so much in C++) there comes a time when you need to talk about objects that aren&#8217;t local to the scope. That&#8217;s when we need dynamic allocation.<\/p>\n<p>The prime example is your typical linked list. The list consists of nodes:<\/p>\n<pre><code>typedef struct node_tmp\n{\n  int data;\n  struct node_tmp * next;\n  struct node_tmp * prev;\n} node;\n<\/code><\/pre>\n<p>Now to talk about such a list boils down to talking about any of its nodes and brachiate along the prev\/next pointers. However, the actual nodes cannot sensibly be part of any local scope, so they are usually <em>dynamically allocated<\/em>:<\/p>\n<pre><code>node * create_list()\n{\n  node * p = malloc(sizeof node);  \/\/ [1]\n  p-&gt;prev = p-&gt;next = 0;\n  return p;\n}\n\nvoid free_list(node * p) \/\/ call with head node\n{\n  while (p-&gt;next)\n  {\n    node * tmp = p;\n    p = p-&gt;next;\n    free(tmp);                     \/\/ [2a]\n  }\n  free(p);                         \/\/ [2b]\n}\n\nvoid append_to_end(node * p, int data); \/\/ etc.\n<\/code><\/pre>\n<p>Here the list nodes exist outside any scope, and you have to bring them to life manually using <code>malloc()<\/code>, and clean them up when you&#8217;re done.<\/p>\n<p>You can use linked lists even in the tiniest of programs, but there&#8217;s no real way around the manual allocation.<\/p>\n<p>Edit: I thought of another example that should really convince you: You might think that you can just make the list with automatically allocated nodes:<\/p>\n<pre><code>node n1, n2, n3;      \/\/ an automatic linked list\nn1.prev = n3.next = 0;\nn1.next = &amp;n2; n2.prev = &amp;n1; n2.next = &amp;n3; n3.prev = &amp;n2;\n<\/code><\/pre>\n<p>But note that you cannot do this dynamically! &#8220;Dynamic&#8221; means &#8220;at runtime&#8221;, but automatic variables have to be determined entirely at compile time.<\/p>\n<p>Suppose you wanted a program that reads integers from the user. If it&#8217;s even, you add it to the list, if it&#8217;s odd you ignore it, and if it&#8217;s zero you stop. You cannot possibly realize such a program with automatic allocation, because the allocation needs are only determined at runtime.<\/p>\n<p>It is in such a scenario that you require <code>malloc()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think the answers are missing an important point. The size of memory is a relatively specific technical detail which isn&#8217;t of primary interest. The crucial difference is that between automatic and dynamic storage, and the associated lifetime: Automatic storage ends at the end of the scope. Dynamic storage begins with malloc() and ends with [&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-4566","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4566","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=4566"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4566\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}