{"id":5098,"date":"2014-03-30T18:46:57","date_gmt":"2014-03-30T18:46:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/should-this-c-pointer-be-released-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:46:57","modified_gmt":"2014-03-30T18:46:57","slug":"should-this-c-pointer-be-released-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/should-this-c-pointer-be-released-collection-of-common-programming-errors\/","title":{"rendered":"Should this C pointer be released?-Collection of common programming errors"},"content":{"rendered":"<p>In C language you don&#8217;t and can&#8217;t &#8220;release&#8221; pointers. Pointers are ordinary scalar variables. There&#8217;s nothing you can do with them in terms of &#8220;releasing&#8221; or anything like that.<\/p>\n<p>What one can be &#8220;releasing&#8221; is <em>the memory a pointer is pointing to<\/em>. But in C you only have to release memory that was explicitly <em>allocated<\/em> by <code>malloc<\/code>\/<code>calloc<\/code>\/<code>realloc<\/code>. Such memory is released by calling <code>free<\/code>.<\/p>\n<p>Note again, that in your program you might have several (a hundred) pointers pointing to <em>the same<\/em> block of allocated memory. Eventually, you&#8217;ll have to release that memory block. But regardless of how many pointers you have pointing to that block, you have to release that memory block exactly once. It is your responsibility to make sure that you release it. And it is your responsibility to make sure you released it exactly once. I&#8217;m telling you this just to illustrate the fact that what you release is the <em>memory block<\/em>, not the pointers.<\/p>\n<p>In your example, your pointer points to a memory block that was never <em>allocated<\/em> by any of the aforementioned functions. This immediately means that you don&#8217;t need to release anything.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C language you don&#8217;t and can&#8217;t &#8220;release&#8221; pointers. Pointers are ordinary scalar variables. There&#8217;s nothing you can do with them in terms of &#8220;releasing&#8221; or anything like that. What one can be &#8220;releasing&#8221; is the memory a pointer is pointing to. But in C you only have to release memory that was explicitly allocated [&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-5098","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5098","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=5098"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5098\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}