{"id":21773,"date":"2008-07-02T10:00:00","date_gmt":"2008-07-02T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/07\/02\/uninitialized-floating-point-variables-can-be-deadly\/"},"modified":"2008-07-02T10:00:00","modified_gmt":"2008-07-02T10:00:00","slug":"uninitialized-floating-point-variables-can-be-deadly","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080702-00\/?p=21773","title":{"rendered":"Uninitialized floating point variables can be deadly"},"content":{"rendered":"<p><P>\nA colleague of mine related to me this story about uninitialized\nfloating point variables.\nHe had a function that went something like this,\nsimplified for expository purposes.\nThe <CODE>infoType<\/CODE> parameter specified which piece of\ninformation you&#8217;re requesting,\nand depending on what you&#8217;re asking for,\none or the other of the output parameters may not contain a\nmeaningful result.\n<\/P>\n<PRE>\nBOOL GetInfo(int infoType, int *intResult, double *dblResult)\n{\n int intValue;\n double dblValue;<\/p>\n<p> switch (infoType) {\n case NUMBER_OF_GLOBS:\n  intValue = &#8230;;\n  break;<\/p>\n<p> case AVERAGE_GLOB_SIZE:\n  dblValue = &#8230;;\n  break;\n &#8230;\n }\n *intResult = intValue;\n *dblResult = dblValue;\n &#8230;\n}\n<\/PRE>\n<P>\nAfter the product shipped, they started geting crash reports.\nThis was in the days before Windows Error Reporting,\nso all they had to work from was the faulting address,\nwhich implicated the line\n<CODE>*dblResult = dblValue<\/CODE>.\n<\/P>\n<P>\nMy colleague initially suspected that <CODE>dblResult<\/CODE>\nwas an invalid pointer, but a search of the entire code base\nruled out that possibility.\n<\/P>\n<P>\nThe problem was the use of an uninitialized floating point\nvariable.\nUnlike integers, not all bit patterns are valid for use as\nfloating point values.\nThere is a category of values\nknown as <I>signaling NaNs<\/I>,\nor\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/06\/11\/8590589.aspx#8592966\">\n<CODE>SNaN<\/CODE> for short<\/A>,\nwhich are special &#8220;not a number&#8221; values.\nIf you ask the processor to,\nit will keep an eye out for these signaling NaNs\nand raise an &#8220;invalid operand&#8221; exception when one\nis encountered.\n(This, after all, is the whole reason why it&#8217;s called a <I>signaling<\/I>\nNaN.)\n<\/P>\n<P>\nThe problem was that, if you are sufficiently unlucky,\nthe leftover values in the memory assigned to the\n<CODE>dblValue<\/CODE> will happen to \nhave a bit pattern corresponding to a <CODE>SNaN<\/CODE>.\nAnd then when the processor tries to copy it to\n<CODE>dblResult<\/CODE>, then exception is raised.\n<\/P>\n<P>\nThere&#8217;s another puzzle lurking behind this one:\nWhy wasn&#8217;t this problem caught in internal testing?\nWe&#8217;ll learn about that next time.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A colleague of mine related to me this story about uninitialized floating point variables. He had a function that went something like this, simplified for expository purposes. The infoType parameter specified which piece of information you&#8217;re requesting, and depending on what you&#8217;re asking for, one or the other of the output parameters may not contain [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-21773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A colleague of mine related to me this story about uninitialized floating point variables. He had a function that went something like this, simplified for expository purposes. The infoType parameter specified which piece of information you&#8217;re requesting, and depending on what you&#8217;re asking for, one or the other of the output parameters may not contain [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=21773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21773\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=21773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=21773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=21773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}