{"id":22,"date":"2011-01-28T10:34:19","date_gmt":"2011-01-28T18:34:19","guid":{"rendered":"http:\/\/ec2-204-236-163-0.us-west-1.compute.amazonaws.com\/blog\/?p=22"},"modified":"2011-02-28T20:15:15","modified_gmt":"2011-03-01T04:15:15","slug":"uninitialized-variable-makes-a-mysterious-bug","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/01\/uninitialized-variable-makes-a-mysterious-bug\/","title":{"rendered":"Uninitialized variable makes a mysterious bug"},"content":{"rendered":"<p><strong>Reason:<\/strong><\/p>\n<p>I used strtod() function in C, forgot to set errno to 0, then after calling strtod(), the value of errno is unpredictable.<\/p>\n<p><strong>When found the bug:<\/strong><\/p>\n<p>I tried to use Ptyhon ctypes module. In script mode, python pyCtypes.py always crash but in command line mode, the code becomes all right. It&#8217;s mysterious running Python in different ways turns out to give different results.<\/p>\n<p>First, I thought it is clear that Python has some bug, otherwise the same Python code should give the same result.<\/p>\n<p>Then I realized my Python code use a DLL routine using C language, and I recalled in the man page of &#8220;strtod&#8221;, it says we need to initialize errno value to zero every time before calling.<\/p>\n<p><strong>Example code:<\/strong><br \/>\nLine 1 should be added to ensure correctness.<\/p>\n<pre class=\"brush: cpp; highlight: [1]; title: ; notranslate\" title=\"\">\r\n        errno = 0;\r\n        vec-&gt;value&#x5B;vec-&gt;len++] = strtod(temp, NULL);\r\n        \/\/vector_print(vec);\r\n        if (errno != 0) {\r\n            perror(&quot;strtod&quot;);\r\n            fprintf(stderr, &quot;%s\\n&quot;, temp);\r\n            exit(EXIT_FAILURE);\r\n        }\r\n<\/pre>\n<p><strong>Note:<br \/>\n <\/strong><\/p>\n<p>WordPress supports syntax highlight.<\/p>\n<p>I am using SyntaxHighlighter Evolved <a title=\"SyntaxHighlighter Evolved\" href=\"http:\/\/wordpress.org\/extend\/plugins\/syntaxhighlighter\/\" target=\"_blank\">http:\/\/wordpress.org\/extend\/plugins\/syntaxhighlighter\/<\/a>.<\/p>\n<p>Official documentation mentions another way: <a href=\"http:\/\/en.support.wordpress.com\/code\/posting-source-code\/\">http:\/\/en.support.wordpress.com\/code\/posting-source-code\/<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reason: I used strtod() function in C, forgot to set errno to 0, then after calling strtod(), the value of errno is unpredictable. When found the bug: I tried to use Ptyhon ctypes module. In script mode, python pyCtypes.py always crash but in command line mode, the code becomes all right. It&#8217;s mysterious running Python [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[142,5],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-code","tag-code","tag-wordpress-2"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}