{"id":8531,"date":"2010-04-01T10:06:09","date_gmt":"2010-04-01T14:06:09","guid":{"rendered":"http:\/\/ok-cleek.com\/blogs\/?p=8531"},"modified":"2010-04-01T10:06:09","modified_gmt":"2010-04-01T14:06:09","slug":"nothrow-new","status":"publish","type":"post","link":"https:\/\/ok-cleek.com\/blogs\/?p=8531","title":{"rendered":"nothrow new"},"content":{"rendered":"<p>I suppose this makes me a bad programmer. Very well.<\/p>\n<p>Programmer geek stuff follows...<br \/>\n<!--more--><\/p>\n<p>Because Visual C++ 6.0 has, until very recently, been my C++ compiler of choice, I've been writing code like this:<\/p>\n<pre>\nCBobRun * pRunBobRun = new CBobRun();\nif (pRunBobRun == NULL) complain;\n<\/pre>\n<p>I did not know that newer compilers will throw an exception if new fails. But now I know. And yes, I learned the hard way.<\/p>\n<p>Deep inside a library I wrote, lives a little memory buffer class. You tell it to allocate a chunk of memory, and it does. When you're done with it, it frees the memory and everybody is happy. This class is the heart of many of my apps. But when I moved one of those apps to Visual Studio 2008, I started getting reports of strange crashes and failures. After a week of digging and working back and forth with one of these users, I found the problem. You can probably guess what it was.<\/p>\n<p>Because this particular user was telling the app to do something way out past the limits of what I expected a user to do, 'new' was failing as it tried to allocate a huge amount of memory; and instead of failing quietly, as I thought it would, new was throwing an exception. Since I wasn't expecting the exception, there was nothing in place to catch it... well, almost nothing. <\/p>\n<p>Way up at the top-level of the app was a \"catch(...)\", and it caught that exception. But it did nothing but spit out a cryptic little error message in response. And, for some reason, it took me four or five days before I could even get the failure to happen. Gah.<\/p>\n<p>So now, via the magic of <a href=\"http:\/\/www.funduc.com\/search_replace.htm\">search & replace<\/a>, I've turned all my news into:<\/p>\n<pre>\nCBobRun * pRunBobRun = new <b>(std::nothrow)<\/b> CBobRun();\nif (pRunBobRun == NULL) complain;\n<\/pre>\n<p>That form of new won't throw anything. <\/p>\n<p>But I wonder what exciting problems <em>this <\/em>will create !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I suppose this makes me a bad programmer. Very well. Programmer geek stuff follows...<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8531","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/8531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8531"}],"version-history":[{"count":0,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/8531\/revisions"}],"wp:attachment":[{"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ok-cleek.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}