{"id":4963,"date":"2021-09-11T00:01:15","date_gmt":"2021-09-11T07:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4963"},"modified":"2021-09-04T11:38:27","modified_gmt":"2021-09-04T18:38:27","slug":"is-it-bad-to-cast-malloc","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4963","title":{"rendered":"Is it Bad to Cast <em>malloc()<\/em>?"},"content":{"rendered":"<p>In my code, in my books, and in my online courses, I typecast the <em>malloc()<\/em> function. This is something I&#8217;ve done for a while, but never really knew why &mdash; until now. I&#8217;ve also learned that doing so is considered &#8220;bad programming practice&#8221; by some in the C community.<br \/>\n<!--more--><br \/>\nThe <em>malloc()<\/em> function allocates a chunk of memory. The function returns the memory chunk&#8217;s address as a <em>void<\/em> pointer: <code>void *<\/code><\/p>\n<p>Somewhere along the line, I started typecasting the <em>malloc()<\/em> function to match the data type of its pointer assignment. So if <code>blorf<\/code> is a <em>char<\/em> pointer, I&#8217;d do this:<\/p>\n<p><code>blorf = (char *)malloc( 1024 );<\/code><\/p>\n<p>This statement allocates a 1024 byte buffer and assigns its address to <em>char<\/em> pointer <code>blorf<\/code>. The <code>(char *)<\/code> typecast transforms the <em>void<\/em> pointer into a <em>char<\/em> pointer for the assignment. Yet, this typecast is unnecessary in the C language.<\/p>\n<p>Somewhere at some time, I probably read that typecasting the return value of <em>malloc()<\/em> was strongly suggested. I forget the specifics, other than I started typecasting <em>malloc()<\/em> out of habit. Then I learned that typecasting <em>malloc()<\/em> is &#8220;bad programming practice,&#8221; though even this assertion is debated.<\/p>\n<p>First, in the C++ language, typecasting <em>malloc()<\/em> is mandatory. Due to the crossover of C and C++ material, this is probably the reason why I started typecasting <em>malloc()<\/em>: I read some C code somewhere written by a C++ programmer who just typecasts <em>malloc()<\/em> for reasons in one language that aren&#8217;t required in the other.<\/p>\n<p>Second, the thing that offends some C programmers the most is that by typecasting <em>malloc()<\/em> you run the risk of improperly identifying the function. In this sense, typecasting works like a mask: When any function is typecast, the compiler may not bother looking up its definition by checking a prototype. I&#8217;m unsure of the specifics, but even on this point I see a lot of back-and-forth between C experts.<\/p>\n<p>In the following code, I typecast <em>malloc()<\/em> without including the <code>stdlib.h<\/code> header file, which is where the function&#8217;s prototype exists:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_09_11-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_09_11-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int *p;\r\n\r\n    p = (int *)malloc( 1024 );\r\n    if( p==NULL )\r\n        printf(\"malloc() failed\\n\");\r\n    else\r\n        printf(\"malloc() succeeded\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program doesn&#8217;t build when using <em>clang<\/em> or <em>gcc<\/em>: Error messages are output, specifically stating that  <em>malloc()<\/em> is a library function and it requires the <code>stdlib.h<\/code> header file. Good for these compilers!<\/p>\n<p>With the MinGW compiler, however, the program builds. Warnings are generated, just like with <em>clang<\/em> and <em>gcc<\/em>: The <em>malloc()<\/em> function requires the <code>stdlib.h<\/code> header file &mdash; but the program builds anyway. And it runs. This result is why some programmers caution against typecasting the <em>malloc()<\/em> function.<\/p>\n<p>Though it isn&#8217;t what I consider a Great Sin, I shan&#8217;t be typecasting <em>malloc()<\/em> in my code from here on. But I also feel it&#8217;s an overreaction to say that typecasting <em>malloc()<\/em> is &#8220;bad&#8221; programming practice. Given the errors and warnings generated by modern compilers, I&#8217;d say typecasting <em>malloc()<\/em> isn&#8217;t bad, just unnecessary and easily avoidable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apparently one of the Great Programming Debates raging currently regards casting of the malloc() function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4963\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4963","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4963"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4963\/revisions"}],"predecessor-version":[{"id":4976,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4963\/revisions\/4976"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}