{"id":504,"date":"2017-06-04T01:46:53","date_gmt":"2017-06-04T09:46:53","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=504"},"modified":"2017-12-12T06:26:32","modified_gmt":"2017-12-12T14:26:32","slug":"c-traps-and-pitfalls","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2017\/06\/04\/c-traps-and-pitfalls\/","title":{"rendered":"C Traps and Pitfalls"},"content":{"rendered":"<p>Here&#8217;s a concise paper describing common C programming pitfalls by Andrew Koening (<a href=\"http:\/\/www.literateprogramming.com\/ctraps.pdf\">www.literateprogramming.com\/ctraps.pdf<\/a>) corresponding to be book with the same title.<\/p>\n<p>As a reminder to myself, I&#8217;ll spend this page summarizing\u00a0common mistakes and my history with it.<\/p>\n<p>Here are the mistakes that I don&#8217;t make because of certain programming habits:<\/p>\n<ul>\n<li>Operator precedence: I use enough parenthesis\u00a0to not rely on operator precedence<\/li>\n<li>Pointer dereferencing: I always do <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">*(p++)<\/code> instead of\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">*p++<\/code> unless it&#8217;s idiomatic.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">for()<\/code> or <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">if()<\/code> statements executing only first line: I always surround the block with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">{}<\/code> even if it&#8217;s just one line. Too often we need to inject an extra line and without {} it becomes a trap.<\/li>\n<li>Undefined side effect order: I never do something like <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">y[i++]=x[i]<\/code><\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">char* p, q<\/code>: very tempting since C++\u00a0style emphasize on pointer as a type over whether the variable is a pointer. I almost never declare multiple variables in one line.<\/li>\n<li>Macro\u00a0repeating side effects: use inline functions instead whenever possible. Use templates in C++.<\/li>\n<li>Unexpected macro associations: guard expressions with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">()<\/code>. Use <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">typedef<\/code>.<\/li>\n<\/ul>\n<p>Did these once before, adjusted my programming habits to avoid it:<\/p>\n<ul>\n<li>counting down in <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">for()<\/code> loop with unsigned running variable: I stick with signed running variables in general. If I&#8217;m forced to use unsigned, I&#8217;ll remind myself that\u00a0I can only stop AFTER hitting 1, but not 0 (i.e. i=0 never got executed).<\/li>\n<\/ul>\n<p>Haven&#8217;t got a chance to run into these, but I&#8217;ll program defensively:<\/p>\n<ul>\n<li>Integer overflow: do <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">a&lt;b<\/code> instead of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">(a-b)&lt;0<\/code>. Calculate mean by adding halfway length to the smaller number (i.e. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">(a+b)\/2 == a + (b-a)\/2<\/code> given <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">a&lt;b<\/code>). Shows up in binary search.<\/li>\n<li>Number of bits to shift is always unsigned (i.e. -1 is a big number!)<\/li>\n<\/ul>\n<p>What I learned from the paper:<\/p>\n<ul>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">stdio<\/code> buffer on stack (registered with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">setbuf()<\/code>) freed before I\/O flushed:\u00a0use static buffer (or just make sure the buffer\u00a0lives outside the function call).<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">char<\/code> type might be signed\u00a0(128\u00a0to 255 are -128\u00a0to -1) so it sign extends during upcast. Use <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">unsigned char<\/code> go guarantee zero extend for upcasting.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">toupper()\/tolower()<\/code> might be implemented as a simple macro (no checks, incorrect \/w side effects)<\/li>\n<li>Can index into a string literal: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">\"abcdefg\"[3]<\/code> gives <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">'d'<\/code><\/li>\n<\/ul>\n<p>Mistakes that I usually make when I switch back from full-time MATLAB programming:<\/p>\n<ul>\n<li>Logical negation using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">~<\/code>\u00a0operator instead of ! operator.<\/li>\n<\/ul>\n<p>Common mistakes I rarely make\u00a0because of certain understanding:<\/p>\n<ul>\n<li>Forgetting to <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">break<\/code> at every\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">case<\/code> in <code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">switch<\/code> block. It&#8217;s hard to forget once you&#8217;re aware of the Duff&#8217;s\u00a0device.<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">sizeof(a[])\/sizeof(a[0])<\/code> after passing into a function does not give array length: hard to get it wrong once you understand\u00a0that array (declared on stack) has <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2017\/11\/25\/understanding-the-difference-between-recognized-arrays-and-pointers\/\">meta-info<\/a> that\u00a0cannot be accessed beyond the stack level it&#8217;s initialized.<\/li>\n<\/ul>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_504\" class=\"pvc_stats all  \" data-element-id=\"504\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a concise paper describing common C programming pitfalls by Andrew Koening (www.literateprogramming.com\/ctraps.pdf) corresponding to be book with the same title. As a reminder to myself, I&#8217;ll spend this page summarizing\u00a0common mistakes and my history with it. Here are the &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2017\/06\/04\/c-traps-and-pitfalls\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_504\" class=\"pvc_stats all  \" data-element-id=\"504\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[30,6,29],"tags":[],"class_list":["post-504","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-note-to-self","category-programming"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=504"}],"version-history":[{"count":13,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":816,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/504\/revisions\/816"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}