{"id":4523,"date":"2014-03-30T13:12:00","date_gmt":"2014-03-30T13:12:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-does-the-malloc-function-work-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:12:00","modified_gmt":"2014-03-30T13:12:00","slug":"how-does-the-malloc-function-work-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-does-the-malloc-function-work-in-c-collection-of-common-programming-errors\/","title":{"rendered":"How does the malloc function work in C?-Collection of common programming errors"},"content":{"rendered":"<p>My goal is to read a file and store it&#8217;s contents in a char array given the offset and number of bytes to be read. I have written a function for doing the same and it works fine.<\/p>\n<p>Now this function is to be called from somewhere else. So I am trying to declare a variable <code>char * data<\/code> which will hold the contents returned by the above mentioned function. After declaring I tried to allocate it some memory. (I know how much, as I specify the number of bytes to be read). It goes as follows:<\/p>\n<pre><code>char * data;\nchar * filename = \"alphabet.txt\";\nint data_size = 10;\nprintf(\"data size: %d\\n\", data_size);\ndata = (char*) malloc (data_size);\nprintf(\"Size allocated to data: %d\\n\",sizeof(data));\nreturn 0;\n<\/code><\/pre>\n<p>This code gives the following output:<\/p>\n<pre><code>data size: 10\nSize allocated to data: 8\n<\/code><\/pre>\n<p>I don&#8217;t understand this behavior. Can somebody please explain it to me.<\/p>\n<p>Thanks a lot<\/p>\n<p>shahensha<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My goal is to read a file and store it&#8217;s contents in a char array given the offset and number of bytes to be read. I have written a function for doing the same and it works fine. Now this function is to be called from somewhere else. So I am trying to declare a [&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-4523","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4523","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=4523"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4523\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}