啟始 Global Variable 的困境
Posted on December 29th, 2008 at 1:17 by fr3@K

Global Variables in C and C++

先簡單的介紹一下 C 與 C++ 處理 global variable initialization 的差異. 下面的 code snip 定義了四個 non-auto variable, 它們的 life scope 都 (可以) 是全局的, 要到程式 termination 才結束. 因此它們都可算是 實質上的 global variable, 本文也將以 global variable 稱之:

    C++:
    1. int global_1 = 1;
    2.  
    3. int* foo()
    4. {
    5.   static int global_2 = 1;
    6.   return &global_2;
    7. }
    8.  
    9. int global_3 = *foo();
    10.  
    11. int* bar()
    12. {
    13.   static int global_4 = *foo();
    14.   return &global_4;
    15. }

以 GCC 為例, 這段 code 可以合法地當作 C++ source code 編譯:

    freak@blizzard:~/tmp$ # when compiled as C++, it went just fine.
    freak@blizzard:~/tmp$
    freak@blizzard:~/tmp$ gcc -x c++ -c globals.c
    freak@blizzard:~/tmp$ 

卻會被 C compiler 拒絕:

    freak@blizzard:~/tmp$ # when compiled as good old C, compiler complains.
    freak@blizzard:~/tmp$
    freak@blizzard:~/tmp$ gcc -x c -c globals.c
    globals.c:9: error: initializer element is not constant
    globals.c: In function ‘bar’:
    globals.c:13: error: initializer element is not constant
    freak@blizzard:~/tmp$ 

(more...)

del.icio.us:啟始 Global Variable 的困境 digg:啟始 Global Variable 的困境 spurl:啟始 Global Variable 的困境 newsvine:啟始 Global Variable 的困境 furl:啟始 Global Variable 的困境 Y!:啟始 Global Variable 的困境 黑米共享書籤:啟始 Global Variable 的困境 推推王:啟始 Global Variable 的困境
邪惡的是 Bad Design
Posted on December 14th, 2008 at 17:55 by fr3@K

幾乎所有 programmer 都知道 evil global variables 這句話. Global variable 的最大問題之一是 scope. 通常一個 variable 的 scope 愈大, 能帶來的 complexity 就愈大.

但現實是, 在任何有意義的程式中, 我們幾乎總是無法避免 global variable. 即便是 C/C++ 的 Standard Library 都提供了 global variable 給 client code 使用, 如 errno, stdin/stdout.
(more...)

del.icio.us:邪惡的是 Bad Design digg:邪惡的是 Bad Design spurl:邪惡的是 Bad Design newsvine:邪惡的是 Bad Design furl:邪惡的是 Bad Design Y!:邪惡的是 Bad Design 黑米共享書籤:邪惡的是 Bad Design 推推王:邪惡的是 Bad Design
Ubuntu GNU/Linux 8.04 -> 8.10
Posted on December 11th, 2008 at 14:13 by fr3@K

從 Debian 轉到 Ubuntu 之後, 因為幾次不好的 distro upgrade 經驗, 我一直都是以 reinstall 來更新 distro. 而我每次更新時都是因為有讓我非跟不可的理由, 因此, reinstall 也沒讓我覺得太麻煩.

上週為了可以貪圖 8.10 內建的 GCC 4.3 以及可以直接使用 E220 連行動網路的 NetworkManager. 雖然 8.04 也是用得好好的, 眼看朋友們 upgrade 都很順利, 我也冒險來了一次 distro upgrade.

用內建的 Upgrade Manager 搞半天也沒見到 distro upgrade 的選項, (Yes, I've googled and read tons of tutorials.) 最後是參考了 Tsung 說明的 把 alternative install CD mount 起來 upgrade 的方法 才能夠開始升級.

我的升級過程很順利, 有幾次需要手動干預的時候是在遇到 config 衝突時, 稍微注意一下 diff 然後排除 conflict.

升級跑完後, reboot, 搞定.

del.icio.us:Ubuntu GNU/Linux 8.04 -> 8.10 digg:Ubuntu GNU/Linux 8.04 -> 8.10 spurl:Ubuntu GNU/Linux 8.04 -> 8.10 newsvine:Ubuntu GNU/Linux 8.04 -> 8.10 furl:Ubuntu GNU/Linux 8.04 -> 8.10 Y!:Ubuntu GNU/Linux 8.04 -> 8.10 黑米共享書籤:Ubuntu GNU/Linux 8.04 -> 8.10 推推王:Ubuntu GNU/Linux 8.04 -> 8.10
Re: G++ 尚未支援 extern template?
Posted on December 8th, 2008 at 0:40 by fr3@K

回應 G++ 尚未支援 extern template? 原文作者的意見:

原來如此,不過的確 header file 跟 implementation file 有時是有分開的必要,那麼,對 function template 來說又該怎麼作比較好?


先認錯一下, 我在該文的回應中數次提到 extern template. 事後我又研究了一下, 是我搞錯了, 應該是 exported template.

兩者為相關但功能不同的 C++ feature.

我的錯誤導致作者把文章的標題改錯了, sorry... 我的回應應更正為:

即便已編譯成 library, 當 library source 內有 extern template exported template 時, client code 依然需要該 extern template template definition 的 source code 才能 link 到 template 的 instance.

Note: gcc 4.3/4.4 的確支援 extern templates.
(more...)

del.icio.us:Re: G++ 尚未支援 extern template? digg:Re: G++ 尚未支援 extern template? spurl:Re: G++ 尚未支援 extern template? newsvine:Re: G++ 尚未支援 extern template? furl:Re: G++ 尚未支援 extern template? Y!:Re: G++ 尚未支援 extern template? 黑米共享書籤:Re: G++ 尚未支援 extern template? 推推王:Re: G++ 尚未支援 extern template?
Re: ‘dynamic_cast’ : ‘A’ is not a polymorphic type
Posted on December 7th, 2008 at 19:20 by fr3@K

回應 'dynamic_cast' : 'A' is not a polymorphic type 原文作者的意見:

Thank you.. :)
其實這個東西以前都只會用,也沒去研究他dynamic_cast怎麼判斷的,而且我都很習慣的在base class加入virtual destructor,所以剛好都沒遇到這個問題 XD


我個人認為應該要先仔細思考, 再決定是否將一個 class type 的 destructor 宣告為 virtual.

如此不但可減少不需要的 overhead. 更重要的是, 那可能是阻止使用者繼承該 class 的最有效手段之一. (請參考我之前留的 link) 畢竟, 不是什麼 class 都是被設計來讓使用者以動態的方式操作的. 以 C++ Standard Library 為例, 除了 IOStream framework 以及 implementor 偷懶的部份之外, 我還真想不起來那些 class 有 C++ Standard 定義的 virtual destructor.

撇開所謂的 framework, 優秀的/重複使用性高的 class library 的特徵之一就是低相依性. 而繼承正是 C++ 能表達的最大 dependency. 就如一把殺傷力強大的手槍一樣, inheritance 與 virtual 本身並不 evil. 會發揮正面功能或是負面影響, 就要看我們是如何使用這些 C++ 給的武器.

del.icio.us:Re: 'dynamic_cast' : 'A' is not a polymorphic type digg:Re: 'dynamic_cast' : 'A' is not a polymorphic type spurl:Re: 'dynamic_cast' : 'A' is not a polymorphic type newsvine:Re: 'dynamic_cast' : 'A' is not a polymorphic type furl:Re: 'dynamic_cast' : 'A' is not a polymorphic type Y!:Re: 'dynamic_cast' : 'A' is not a polymorphic type 黑米共享書籤:Re: 'dynamic_cast' : 'A' is not a polymorphic type 推推王:Re: 'dynamic_cast' : 'A' is not a polymorphic type
Thank You, Cindy and Samantha
Posted on December 4th, 2008 at 22:47 by fr3@K

A special b-day present from Cindy and Samantha:

So sweet, I really enjoy it. :)

I Love you~~~

del.icio.us:Thank You, Cindy and Samantha digg:Thank You, Cindy and Samantha spurl:Thank You, Cindy and Samantha newsvine:Thank You, Cindy and Samantha furl:Thank You, Cindy and Samantha Y!:Thank You, Cindy and Samantha 黑米共享書籤:Thank You, Cindy and Samantha 推推王:Thank You, Cindy and Samantha
How Bad Do You Want It?
Posted on December 2nd, 2008 at 23:55 by fr3@K

From The Last Lecture by professor Randy Pausch:

眼前的高牆不是為了阻擋我們前進,

而是讓我們有機會展示自己,

確認自己有多想完成夢想.

好想, 我真的好想好想.

推荐延伸閱讀: 朱學恆的一篇 介紹 Randy Pausch 教授生平事蹟的文章.

del.icio.us:How Bad Do You Want It? digg:How Bad Do You Want It? spurl:How Bad Do You Want It? newsvine:How Bad Do You Want It? furl:How Bad Do You Want It? Y!:How Bad Do You Want It? 黑米共享書籤:How Bad Do You Want It? 推推王:How Bad Do You Want It?