{"id":3183,"date":"2018-07-14T00:01:15","date_gmt":"2018-07-14T07:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3183"},"modified":"2018-06-30T13:30:16","modified_gmt":"2018-06-30T20:30:16","slug":"lets-be-assertive","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3183","title":{"rendered":"Let&#8217;s Be Assertive"},"content":{"rendered":"<p>I find it fun to debug code. I add various <em>printf()<\/em> statements to a program to display values and quickly determine what&#8217;s going on. This process seems easier than toiling with a debugger.<br \/>\n<!--more--><br \/>\nFor example, and especially with pointers, when I see a memory location changing between two functions, I use <em>printf()<\/em> statements to display the pointer&#8217;s memory address and value in each function:<\/p>\n<p><code>printf(\"Address %p, Value %d\\n\",ptr,*ptr);<\/code><\/p>\n<p>These statements help me unravel mysteries in the code and eventually fix the issue.<\/p>\n<p>Another way to spot bugs in you code is to use <em>assert()<\/em>.<\/p>\n<p>The <em>assert()<\/em> macro is defined in the <code>assert.h<\/code> header. You may have seen it in some code samples, though most coders pull it out when they&#8217;re done testing. What <em>assert()<\/em> does is to perform a simple test, to verify a variable or condition. If the condition isn&#8217;t met (is false), the program halts.<\/p>\n<p>In the following code, the <em>assert()<\/em> macro appears at Line 10. It tests the value of variable <code>x<\/code>. As long as <code>x<\/code> is less than 7, <em>assert()<\/em> is good with the code; the value is asserted to be less than 7. When <code>x<\/code> hits 7, however, the program halts.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;assert.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;10;x++)\r\n    {\r\n        assert(x&lt;7);\r\n        printf(\"%d\\n\",x);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\nAssertion failed: (x<7), function main, file 0714.c, line 10.\r\nAbort trap: 6<\/code><\/pre>\n<p>I find the details displayed by the <em>assert()<\/em> macro to be most useful: The condition is displayed, <code>(x<7)<\/code>; the function, <em>main()<\/em>; the filename, <code>0714.c<\/code>, and best of all, the line number <code>10<\/code>. (The text <code>Abort trap<\/code> may not appear in your output.)<\/p>\n<p>You can deploy the <em>assert()<\/em> macro at various points of your code to ensure that variables are properly initialized. Even so, keep in mind that this macro is for your purposes as the programmer to aid in debugging. It's not intended to trap errors that a user may see.<\/p>\n<p>For example, if a chunk of memory can't be allocated, I would use a standard <em>if<\/em> test instead of assert:<\/p>\n<pre><code>if(p==NULL)\r\n\/* DO handle condition here *\/<\/code><\/pre>\n<p>And not:<\/p>\n<pre><code>assert(p!=NULL);     \/* DON'T *\/<\/code><\/pre>\n<p>Further, if you want to keep the <em>assert()<\/em> tests in your code, or you forget where they're located in some massive, multi-line program monster, you can disable them by adding the following constant definition:<\/p>\n<p><code>#define NDEBUG<\/code><\/p>\n<p>When <code>NDEBUG<\/code> is defined in your source code file, the <code>assert.h<\/code> header file rubs out the <em>assert()<\/em> macro definition. So you can switch <em>assert()<\/em> off by adding the above preprocessor directive or turn <em>assert()<\/em> back on by removing the line or commenting it out.<\/p>\n<p>Consider the <em>assert()<\/em> macro yet another arrow in your C language debugging quiver.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Debugging is a major issue for any programmer. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3183\">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-3183","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\/3183","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=3183"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3183\/revisions"}],"predecessor-version":[{"id":3194,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3183\/revisions\/3194"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}