啟始 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++:
-
int global_1 = 1;
-
-
int* foo()
-
{
-
static int global_2 = 1;
-
return &global_2;
-
}
-
-
int global_3 = *foo();
-
-
int* bar()
-
{
-
static int global_4 = *foo();
-
return &global_4;
-
}
以 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...)
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, 搞定.
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...)
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 教授生平事蹟的文章.