<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: How To Handle new Operator Failure</title>
	<atom:link href="http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/feed/" rel="self" type="application/rss+xml" />
	<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/</link>
	<description>Sharing my ideas</description>
	<lastBuildDate>Fri, 27 Nov 2009 08:11:02 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: div</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-148</link>
		<dc:creator>div</dc:creator>
		<pubDate>Fri, 04 Sep 2009 12:04:17 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-148</guid>
		<description>I have query, the requirement is i have to use 2 new operators in a single try block.If an exception occurs for 2nd new, then how do i delete the memory space allocated for 1st new.What is the best way to have the catch implementation.</description>
		<content:encoded><![CDATA[<p>I have query, the requirement is i have to use 2 new operators in a single try block.If an exception occurs for 2nd new, then how do i delete the memory space allocated for 1st new.What is the best way to have the catch implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhrajyoti</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-131</link>
		<dc:creator>Abhrajyoti</dc:creator>
		<pubDate>Wed, 08 Apr 2009 05:09:20 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-131</guid>
		<description>Hello Guys,
Is anybody has idea how the try catch is actually implemented?</description>
		<content:encoded><![CDATA[<p>Hello Guys,<br />
Is anybody has idea how the try catch is actually implemented?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cppkid</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-74</link>
		<dc:creator>cppkid</dc:creator>
		<pubDate>Mon, 19 Jan 2009 07:53:38 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-74</guid>
		<description>Thanks a lot Brenden...
I expect your support in the future also....</description>
		<content:encoded><![CDATA[<p>Thanks a lot Brenden&#8230;<br />
I expect your support in the future also&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brendan Leber</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-69</link>
		<dc:creator>Brendan Leber</dc:creator>
		<pubDate>Fri, 16 Jan 2009 18:32:11 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-69</guid>
		<description>According to the documentation the definition of the handle function is &quot;int __cdecl handler_func(size_t cb)&quot;.  The cb parameter contains the number of bytes that were requested by the allocation.  If the handler function returns non-zero the allocation is retried, otherwise the allocation fails.

I would do as you suggest and attempt to free some memory in the handler and return non-zero to give the system a chance to allocate the memory.  If there isn&#039;t any more memory to free then return zero and let the allocation fail.  I spend a lot of time tracking down failures in systems and I recommend that you always let the system fail as close to the actual problem as possible.  This allows you to quickly find the problem with tools like WinDBG.

Now to the other subject, exception handling.  Yes, exception handling creates more instructions in your executable.  However, we&#039;re talking about exceptional conditions and not the normal execution path.  The gains from writing code that is maintainable far outweigh any early optimization.  If you have a problem with execution speed it would be better to profile your application to find the trouble areas and only optimize them.

These are good posts and you should keep going.  I just want to give you, and your readers, food for thought.</description>
		<content:encoded><![CDATA[<p>According to the documentation the definition of the handle function is &#8220;int __cdecl handler_func(size_t cb)&#8221;.  The cb parameter contains the number of bytes that were requested by the allocation.  If the handler function returns non-zero the allocation is retried, otherwise the allocation fails.</p>
<p>I would do as you suggest and attempt to free some memory in the handler and return non-zero to give the system a chance to allocate the memory.  If there isn&#8217;t any more memory to free then return zero and let the allocation fail.  I spend a lot of time tracking down failures in systems and I recommend that you always let the system fail as close to the actual problem as possible.  This allows you to quickly find the problem with tools like WinDBG.</p>
<p>Now to the other subject, exception handling.  Yes, exception handling creates more instructions in your executable.  However, we&#8217;re talking about exceptional conditions and not the normal execution path.  The gains from writing code that is maintainable far outweigh any early optimization.  If you have a problem with execution speed it would be better to profile your application to find the trouble areas and only optimize them.</p>
<p>These are good posts and you should keep going.  I just want to give you, and your readers, food for thought.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cppkid</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-67</link>
		<dc:creator>cppkid</dc:creator>
		<pubDate>Fri, 16 Jan 2009 03:32:55 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-67</guid>
		<description>Thanks Brendan for your interest in my blog and valuable support.

I just used an exit(0) in code as an example only (in case somebody copies the code and runs it won&#039;t get a system hang). It is not a real life scenario , just an example I could mold out easily. I don&#039;t think you will ever use exit(0) in this place.

Also, I never said try...catch is a bad alternative in this situation (of course, it is something that most of the programmers, including me, prefer. I just said a general fact that try..catch calls are expensive.  I just wanted to show another way to handle new operator failure. (may be my words are mis interpreted).

And finally, what should we do if new operator fails - it is something that still I have to get a concrete idea. May be we can cause the program to wait until more memory is freed by the system when other programs exit or in the worst case, we can log it and exit. If you have some idea, please feel free to cmment it here.

Once again, thanks for your suggestions and comments. Please keep watching the blog.</description>
		<content:encoded><![CDATA[<p>Thanks Brendan for your interest in my blog and valuable support.</p>
<p>I just used an exit(0) in code as an example only (in case somebody copies the code and runs it won&#8217;t get a system hang). It is not a real life scenario , just an example I could mold out easily. I don&#8217;t think you will ever use exit(0) in this place.</p>
<p>Also, I never said try&#8230;catch is a bad alternative in this situation (of course, it is something that most of the programmers, including me, prefer. I just said a general fact that try..catch calls are expensive.  I just wanted to show another way to handle new operator failure. (may be my words are mis interpreted).</p>
<p>And finally, what should we do if new operator fails &#8211; it is something that still I have to get a concrete idea. May be we can cause the program to wait until more memory is freed by the system when other programs exit or in the worst case, we can log it and exit. If you have some idea, please feel free to cmment it here.</p>
<p>Once again, thanks for your suggestions and comments. Please keep watching the blog.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brendan Leber</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-66</link>
		<dc:creator>Brendan Leber</dc:creator>
		<pubDate>Thu, 15 Jan 2009 20:29:13 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-66</guid>
		<description>You didn&#039;t show how to handle new failing.  What do you suggest we do if new fails?  If you&#039;re just going to exit the program it would be better to let it crash at the point of failure.  That way you can zero in on the exact problem instead of trying to figure out which new statement failed.

Why do you think exception handling is expensive?  Which is more expensive handling exceptions or tracking down all of the bugs caused by objects not being deconstructed properly when the exception handler unwinds the stack?</description>
		<content:encoded><![CDATA[<p>You didn&#8217;t show how to handle new failing.  What do you suggest we do if new fails?  If you&#8217;re just going to exit the program it would be better to let it crash at the point of failure.  That way you can zero in on the exact problem instead of trying to figure out which new statement failed.</p>
<p>Why do you think exception handling is expensive?  Which is more expensive handling exceptions or tracking down all of the bugs caused by objects not being deconstructed properly when the exception handler unwinds the stack?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cppkid</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-64</link>
		<dc:creator>cppkid</dc:creator>
		<pubDate>Thu, 15 Jan 2009 12:05:52 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-64</guid>
		<description>Indeed. I just used an example to show the use of  &lt;strong&gt;set_new_handler&lt;/strong&gt;.
However, I think try...catch calls are expensive in usage of resources.
Anyway, the real advantage is that you have to define the handler function for new operator failure only once compared to the fact that you need try...catch for &lt;strong&gt;each &lt;/strong&gt;block of &lt;strong&gt;new&lt;/strong&gt; operators as in

try
{
   new int[100];
}
catch(...)
{
   // Call some routine
}
.
.
.
try
{
   new char[100];
}
catch(...)
{
   // Call some routine
}

Thanks for the comment. Please keep watching the blog.</description>
		<content:encoded><![CDATA[<p>Indeed. I just used an example to show the use of  <strong>set_new_handler</strong>.<br />
However, I think try&#8230;catch calls are expensive in usage of resources.<br />
Anyway, the real advantage is that you have to define the handler function for new operator failure only once compared to the fact that you need try&#8230;catch for <strong>each </strong>block of <strong>new</strong> operators as in</p>
<p>try<br />
{<br />
   new int[100];<br />
}<br />
catch(&#8230;)<br />
{<br />
   // Call some routine<br />
}<br />
.<br />
.<br />
.<br />
try<br />
{<br />
   new char[100];<br />
}<br />
catch(&#8230;)<br />
{<br />
   // Call some routine<br />
}</p>
<p>Thanks for the comment. Please keep watching the blog.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ferruccio</title>
		<link>http://cppkid.wordpress.com/2009/01/15/how-to-handle-new-operator-failure/#comment-62</link>
		<dc:creator>Ferruccio</dc:creator>
		<pubDate>Thu, 15 Jan 2009 11:20:26 +0000</pubDate>
		<guid isPermaLink="false">http://cppkid.wordpress.com/?p=255#comment-62</guid>
		<description>Another possibility is catching a bad_alloc exception. i.e:

try
{
    new int[5000000];
    cout &lt;&lt; &quot;successful so far&quot; &lt;&lt; endl;
}
catch (bad_alloc)
{
    cout &lt;&lt; &quot;out of memory&quot; &lt;&lt; endl;
    exit(0);
}</description>
		<content:encoded><![CDATA[<p>Another possibility is catching a bad_alloc exception. i.e:</p>
<p>try<br />
{<br />
    new int[5000000];<br />
    cout &lt;&lt; &quot;successful so far&quot; &lt;&lt; endl;<br />
}<br />
catch (bad_alloc)<br />
{<br />
    cout &lt;&lt; &quot;out of memory&quot; &lt;&lt; endl;<br />
    exit(0);<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
