{"id":404,"date":"2008-03-30T20:17:04","date_gmt":"2008-03-31T01:17:04","guid":{"rendered":"http:\/\/robocup.bowdoin.edu\/blog\/2008\/03\/30\/c-programming-woes\/"},"modified":"2008-03-30T20:17:04","modified_gmt":"2008-03-31T01:17:04","slug":"c-programming-woes","status":"publish","type":"post","link":"https:\/\/research.bowdoin.edu\/robocup\/robocup\/c-programming-woes\/","title":{"rendered":"C++ programming woes"},"content":{"rendered":"<p>It took me hours and hours to find this bug today. The following code works (even though in my opinion it shouldn&#8217;t). The code is more pseudo- than real. The real thing wouldn&#8217;t be understandable anyway.<\/p>\n<p><code><br \/>\nMatrix *m1 = (Matrix*)malloc(sizeof(Matrix));<br \/>\nfree(m1);<br \/>\nuseMatrix(m1);<br \/>\n<\/code><\/p>\n<p>The code did exactly what it was supposed to do even though I free up the memory in between the calls in a more complicated scenario. However, when I modified the code to this<\/p>\n<p><code><br \/>\nMatrix *m1 = (Matrix*)malloc(sizeof(Matrix));<br \/>\nfree(m1);<br \/>\nMatrix *m2 = (Matrix*)malloc(sizeof(Matrix));<br \/>\nuseMatrix(m1);<br \/>\n<\/code><\/p>\n<p>I got a segfault and couldn&#8217;t figure out why creating a new object would cause my program to crash. In my opinion a more sensible scenario would be that the first code snippet would cause the program to segfault, so I knew that there was something wrong already.<\/p>\n<p>Bizzare&#8230; The platform is OS X Leopard with gcc 4.0.1. The current theory is that even though I free the memory, the contents are still intact and the OS is able to use them, but once I malloc again, the same region of memory got overwritten and the old pointer was marked invalid. I don&#8217;t know&#8230;<\/p>\n<p>Anyone who comes across this and has a good way of explaining this awful behavior to me, I would be happy to hear it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It took me hours and hours to find this bug today. The following code works (even though in my opinion it shouldn&#8217;t). The code is more pseudo- than real. The real thing wouldn&#8217;t be understandable anyway. Matrix *m1 = (Matrix*)malloc(sizeof(Matrix)); free(m1); useMatrix(m1); The code did exactly what it was supposed to do even though I [&hellip;]<\/p>\n","protected":false},"author":56,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-robocup"],"_links":{"self":[{"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/comments?post=404"}],"version-history":[{"count":0,"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/posts\/404\/revisions"}],"wp:attachment":[{"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/media?parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/categories?post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/research.bowdoin.edu\/robocup\/wp-json\/wp\/v2\/tags?post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}