{"id":5395,"date":"2014-03-30T21:25:09","date_gmt":"2014-03-30T21:25:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/memory-allocation-calloc-malloc-for-unsigned-int-collection-of-common-programming-errors\/"},"modified":"2014-03-30T21:25:09","modified_gmt":"2014-03-30T21:25:09","slug":"memory-allocation-calloc-malloc-for-unsigned-int-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/memory-allocation-calloc-malloc-for-unsigned-int-collection-of-common-programming-errors\/","title":{"rendered":"Memory allocation (calloc, malloc) for unsigned int-Collection of common programming errors"},"content":{"rendered":"<p>calloc returns <code>void*<\/code> so you should use it like<\/p>\n<pre><code>unsigned int* part1 = calloc (1,sizeof(*part1));\n<\/code><\/pre>\n<p>then assign it like<\/p>\n<pre><code>*part1 = 42;\n<\/code><\/pre>\n<p>If you have allocated space for several elements<\/p>\n<pre><code>part1[0] = 42; \/\/ valid indices are [0..nmemb-1]\n<\/code><\/pre>\n<p>may be clearer.<\/p>\n<p>Note that you also have to <code>free<\/code> this memory later<\/p>\n<pre><code>free(part1);\n<\/code><\/pre>\n<p>Alternatively, if you only need a single element, just declare it on the stack<\/p>\n<pre><code>unsigned int part1 = 42;\n<\/code><\/pre>\n<p>Regarding why casting a point to <code>unsigned long<\/code> doesn&#8217;t generate a warning, <code>sizeof(void*)==sizeof(unsigned long)<\/code> on your platform. Your code would not be portable if you relied on this. More importantly, if you use a pointer to store a single integer, you&#8217;d leak your newly allocated memory and be unable to ever store more than one element of an array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>calloc returns void* so you should use it like unsigned int* part1 = calloc (1,sizeof(*part1)); then assign it like *part1 = 42; If you have allocated space for several elements part1[0] = 42; \/\/ valid indices are [0..nmemb-1] may be clearer. Note that you also have to free this memory later free(part1); Alternatively, if you [&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-5395","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5395","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=5395"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5395\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}