<?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/"
		>
<channel>
	<title>Comments on: Goto, and intermixed declarations and statements</title>
	<atom:link href="http://www.jeffhung.net/blog/articles/jeffhung/1245/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/</link>
	<description>(My smile insists of having nose. :-)</description>
	<lastBuildDate>Thu, 03 Nov 2011 00:43:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2-alpha</generator>
	<item>
		<title>By: jeffhung</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/comment-page-1/#comment-129987</link>
		<dc:creator>jeffhung</dc:creator>
		<pubDate>Thu, 05 Feb 2009 07:39:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/?p=1245#comment-129987</guid>
		<description>&lt;p&gt;剛剛把某專案拿回 VC6 編譯，發覺新加的功能，中了一堆這種陷阱。在 goto 滿天飛的程式裡加功能，還真是危險啊。&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>剛剛把某專案拿回 VC6 編譯，發覺新加的功能，中了一堆這種陷阱。在 goto 滿天飛的程式裡加功能，還真是危險啊。</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: av</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/comment-page-1/#comment-128933</link>
		<dc:creator>av</dc:creator>
		<pubDate>Mon, 19 Jan 2009 04:13:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/?p=1245#comment-128933</guid>
		<description>我的一個原則：能使用 smart pointer 就儘量用 smart pointer. 一般人只想到 leak 問題，所以覺得只要記得 delete/release resource 就行了，但如果在該 scope 有多個退出點時，很容易就漏掉，例如 Jeff 講的情況：exception.
另外就是，我比較喜歡用 Loki 的 smart pointer, 因為它跟本是萬用的，不只是 memory 可以靠他管理，COM 這種 add/release reference 的可以，就連其他資源管理也通通可以，例如 LoadLibrary/FreeLibray、network connection、反正任何一種需要生成/消滅或是初始/結束的，都可以用它。</description>
		<content:encoded><![CDATA[<p>我的一個原則：能使用 smart pointer 就儘量用 smart pointer. 一般人只想到 leak 問題，所以覺得只要記得 delete/release resource 就行了，但如果在該 scope 有多個退出點時，很容易就漏掉，例如 Jeff 講的情況：exception.<br />
另外就是，我比較喜歡用 Loki 的 smart pointer, 因為它跟本是萬用的，不只是 memory 可以靠他管理，COM 這種 add/release reference 的可以，就連其他資源管理也通通可以，例如 LoadLibrary/FreeLibray、network connection、反正任何一種需要生成/消滅或是初始/結束的，都可以用它。</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arlo</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/comment-page-1/#comment-128813</link>
		<dc:creator>Arlo</dc:creator>
		<pubDate>Sun, 18 Jan 2009 04:58:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/?p=1245#comment-128813</guid>
		<description>哈哈哈哈, 慣C...</description>
		<content:encoded><![CDATA[<p>哈哈哈哈, 慣C...</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeffhung</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/comment-page-1/#comment-128720</link>
		<dc:creator>jeffhung</dc:creator>
		<pubDate>Sat, 17 Jan 2009 02:54:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/?p=1245#comment-128720</guid>
		<description>&lt;p&gt;Lemon,&lt;/p&gt;
&lt;p&gt;要是在 /* do something you want. */ 裡面 throw 了怎麼辦？所以我們可以利用 boost::scoped_ptr 或是 std::auto_ptr，來達到「更有結構性、更易懂、更直觀」：&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;
bool init_objects()
{
    // boost::scoped_ptr guarantees delete holded pointer when destruct.
    // std::auto_ptr may suite here, too, depend on how we use it.
    boost::scoped_ptr&lt;int&gt; k1(new int);
    boost::scoped_ptr&lt;int&gt; k2(new int);
    boost::scoped_ptr&lt;int&gt; k3(new int);
    boost::scoped_ptr&lt;int&gt; k4(new int);
    boost::scoped_ptr&lt;int&gt; kN(new int);

    // do something you want.

    return 0;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Lemon,</p>
<p>要是在 /* do something you want. */ 裡面 throw 了怎麼辦？所以我們可以利用 boost::scoped_ptr 或是 std::auto_ptr，來達到「更有結構性、更易懂、更直觀」：</p>
<pre class="code">
bool init_objects()
{
    // boost::scoped_ptr guarantees delete holded pointer when destruct.
    // std::auto_ptr may suite here, too, depend on how we use it.
    boost::scoped_ptr&lt;int&gt; k1(new int);
    boost::scoped_ptr&lt;int&gt; k2(new int);
    boost::scoped_ptr&lt;int&gt; k3(new int);
    boost::scoped_ptr&lt;int&gt; k4(new int);
    boost::scoped_ptr&lt;int&gt; kN(new int);

    // do something you want.

    return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lemon</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/comment-page-1/#comment-128689</link>
		<dc:creator>Lemon</dc:creator>
		<pubDate>Fri, 16 Jan 2009 17:34:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/?p=1245#comment-128689</guid>
		<description>咳咳~ goto 大概不是一無事處...
以下的 code, 如果有更有結構性, 更易懂, 更直觀的寫法的話, goto 的優點就又少一項了.
武器沒有好或壞之分,完全是看誰在使用,怎麼使用...


bool init_objects()
{
	int* k1;
	int* k2;
	int* k3;
	int* k4;
	int* kN;

	k1= new int;
	if(NULL == k1) goto err_exit;

	k2= new int;
	if(NULL == k2) goto err_free_k1;

	k3= new int;
	if(NULL == k3) goto err_free_k2;

	k4= new int;
	if(NULL == k4) goto err_free_k3;

	kN= new int;
	if(NULL == kN) goto err_free_k4;

	// do something you want.

	// free all allocated object.
err_free_kN:
	delete kN;
	kN= NULL;

err_free_k4:
	delete k4;
	k4= NULL;

err_free_k3:
	delete k3;
	k3= NULL;

err_free_k2:
	delete k2;
	k2= NULL;

err_free_k1:
	delete k1;
	k1= NULL;

err_exit:

	return 0;
}
</description>
		<content:encoded><![CDATA[<p>咳咳~ goto 大概不是一無事處...<br />
以下的 code, 如果有更有結構性, 更易懂, 更直觀的寫法的話, goto 的優點就又少一項了.<br />
武器沒有好或壞之分,完全是看誰在使用,怎麼使用...</p>
<p>bool init_objects()<br />
{<br />
	int* k1;<br />
	int* k2;<br />
	int* k3;<br />
	int* k4;<br />
	int* kN;</p>
<p>	k1= new int;<br />
	if(NULL == k1) goto err_exit;</p>
<p>	k2= new int;<br />
	if(NULL == k2) goto err_free_k1;</p>
<p>	k3= new int;<br />
	if(NULL == k3) goto err_free_k2;</p>
<p>	k4= new int;<br />
	if(NULL == k4) goto err_free_k3;</p>
<p>	kN= new int;<br />
	if(NULL == kN) goto err_free_k4;</p>
<p>	// do something you want.</p>
<p>	// free all allocated object.<br />
err_free_kN:<br />
	delete kN;<br />
	kN= NULL;</p>
<p>err_free_k4:<br />
	delete k4;<br />
	k4= NULL;</p>
<p>err_free_k3:<br />
	delete k3;<br />
	k3= NULL;</p>
<p>err_free_k2:<br />
	delete k2;<br />
	k2= NULL;</p>
<p>err_free_k1:<br />
	delete k1;<br />
	k1= NULL;</p>
<p>err_exit:</p>
<p>	return 0;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: av</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/1245/comment-page-1/#comment-128672</link>
		<dc:creator>av</dc:creator>
		<pubDate>Fri, 16 Jan 2009 08:00:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/?p=1245#comment-128672</guid>
		<description>這問題我也碰過，不過是在還沒真正造成 bug 前就被我發現了，是別的同事寫的 code，我因為本能的看到 goto 就反感了起來，因此還沒造成 bug 前就被我挑出來了。
我的原則是直接不用 goto，除非是臨時的測試用的 code，為了省事才有可能會去用。</description>
		<content:encoded><![CDATA[<p>這問題我也碰過，不過是在還沒真正造成 bug 前就被我發現了，是別的同事寫的 code，我因為本能的看到 goto 就反感了起來，因此還沒造成 bug 前就被我挑出來了。<br />
我的原則是直接不用 goto，除非是臨時的測試用的 code，為了省事才有可能會去用。</p>
]]></content:encoded>
	</item>
</channel>
</rss>

