{"id":1395,"date":"2015-11-09T21:57:03","date_gmt":"2015-11-09T20:57:03","guid":{"rendered":"http:\/\/netrix.org.pl\/?p=1395"},"modified":"2015-12-22T20:47:30","modified_gmt":"2015-12-22T19:47:30","slug":"c-problems-1","status":"publish","type":"post","link":"https:\/\/netrix.org.pl\/index.php\/2015\/11\/09\/c-problems-1\/","title":{"rendered":"C++ problems #1"},"content":{"rendered":"<p>Two days ago I was working on an old neural network project when some strange bug occured in my code. I was working on debug version of the project with no optimizations (-O0) and then I tried to switch them on to see what will be the gain in performance.<\/p>\n<p>What happened is that it stopped working properly. With no optimizations it worked correctly, giving expected results but with -O2 flag it somehow stopped earlier than expected. After some debugging I&#8217;ve managed to find the place where the bug was hidden. This is the place:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\nsize_t getOutputLayerSize() const\r\n{\r\n    m_forwardNetwork.getOutputLayerSize();\r\n}\r\n<\/pre>\n<p>The bug here should be pretty obvious but when running through more code it may well hidden. The obvious thing is that there&#8217;s no <em>return<\/em> statement while the function is defined as returning an unsigned integer value. This code compiles because it may be a valid behaviour in some other cases like following:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\nsize_t getOutputLayerSize() const\r\n{\r\n    throw std::runtime_error(\"\");\r\n}\r\n<\/pre>\n<p>But when nothing is thrown and there is no <em>return<\/em> statement this is an undefined behaviour and should be avoided. <\/p>\n<p>Why it worked in my case with no optimizations? The <em>m_forwardNetwork.getOutputLayerSize()<\/em> function call returns an integer value that is saved in <em>eax<\/em> register. The same register is used in <em>getOutputLayerSize()<\/em> function to keep the return value which is normally saved by the return statement. With no return statement there is no action but since the value was written in <em>m_forwardNetwork.getOutputLayerSize()<\/em> it can be used by the caller of <em>getOutputLayerSize()<\/em> function so it happens to work correctly from caller POV. When optimizations are switched on, the line <em>m_forwardNetwork.getOutputLayerSize()<\/em> is removed by the compiler because it is not used so there is no function call which means <em>eax<\/em> register is not written to. In my case it happened that the overal function call return 0 value which wasn&#8217;t expected.<\/p>\n<p>My fault was that I haven&#8217;t used any warning flags. By adding -Wall compiler starts to emit following warning:<\/p>\n<pre lang=\"cpp\">\r\nwarning: no return statement in function returning non-void [-Wreturn-type]\r\n<\/pre>\n<p>My advice is to always add <em>-Wall<\/em> to compilation flags so no error will be omitted. Another advice is to also add a <em>-Werror<\/em> flag which will force the compiler to fail the compilation if any warning occurs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two days ago I was working on an old neural network project when some strange bug occured in my code. I was working on debug version of the project with no optimizations (-O0) and then I tried to switch them on to see what will be the gain in performance. What happened is that it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[192,179,191],"_links":{"self":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1395"}],"collection":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1395"}],"version-history":[{"count":14,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1395\/revisions"}],"predecessor-version":[{"id":1436,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1395\/revisions\/1436"}],"wp:attachment":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}