2013年9月15日 星期日

nvidia driver for ubuntu

Ubuntu 在 2010 之後的 distribution 所包的 NV driver 是 xserver-
xorg-video-nouveau,但這個 driver 不知道是有 bug 還是怎樣,我的 GT240 會當掉開不進系統,測試了一下,連 Live CD 都沒辦法開進去,網路上有很多 NV 卡都有一樣的問題。目前的處理方式就是把這 driver 移掉,用 nvidia-current 這個 driver。

$ sudo apt-get remove --purge nouveau-firmware
$ sudo apt-get remove --purge xserver-
xorg-video-nouveau
$ sudo apt-get install nvidia-current

2013年8月27日 星期二

Android develop resource

要擁有 Android 的開發環境大致上有兩種方法,
其一就是下載官方包好的工具如 ADTAndroid Studio 這樣的工具。

另外如果本身就有在用 Eclipse 的話,就直接在 Eclipse 中安裝 ADT plugin
再把 Android SDK 跟 NDK 設好就行了。

ADT plugin:
https://dl-ssl.google.com/android/eclipse/  (Install new software at Eclipse)

Android SDK:
http://developer.android.com/sdk/index.html

NDK:
http://developer.android.com/tools/sdk/ndk/index.html

nvidia display driver on ubutu

ubuntu 在這幾年的版本中,預設是使用 xserver-xorg-video-nouveau 這個顯示的 driver,
但這個 driver 似乎還沒有很成熟,如果是使用 nvidia 的卡,可能連桌面都開不進去。

解決的方法就是改用 nvidia-current,這是個虛擬套件,會自動幫你裝最適當的 nv drive。

先想辦法用 Live CD 或 USB 開機,開個 shell 出來。
#sudo apt-get --purge remove nouveau-firmware xserver-xorg-video-nouveau
#sudo apt-get install nvidia-current


參考 https://bugs.launchpad.net/ubuntu/+source/linux/+bug/763052

2011年5月30日 星期一

Boost 之 optional, assign 及 tribool

Boost 提供了一些小工具,讓程式碼可以更俱可讀性。

1. optional
    在寫程式時,常常會需要計算一些數值, 而這些數值有可能在某些條件下是沒有意義的,例如我們要算一個數的平方根時,一定得先檢查此數是否為非負數,若為負數,則此取平方根是沒有意義的(當然若是運算有考慮虛數則例外)。這時我們的程式片段大概長這樣:
    // double value = SOME_VALUE;
    ...
    if (value>=0) 
    {
       double sqrt_value = sqrt (value) ;
    }
    else
    {
        // error handling
    }
    
    這樣看起來其實沒什麼問題,但就設計哲學而言,client端 (sqrt函式的呼叫端) 必需先檢驗"條件",若通過後再真的"求值"。這樣的設計較為危險,因為 client必須去看文件,了解該函式的 domain 為何,再檢查 input 是否在該 domain 內,若是,再取值。把這樣的"知識"交由呼叫端來處理,可能帶來一些淺在的問題,例如呼叫端可能忘記檢查、可能檢查的條件是錯的,可能傳了負數進去得到了exception 卻沒處理,可能
    Boost 提供了一個小小的 wrapper class template 名為 boost::optional 來處理這種無意義值。使用上很簡單,client 以要封裝的 type 為 optional 的 template 參數宣告一個物件,將條件及求值式傳入 constructor。然後我們可以把這個 optional 物件像是測試 NULL pointer 一樣檢查其有效性 (支援 implicit conversion to bool),再用 operator* 取值 (因為 override 了 operator*) 。
    最後一步,就是在 library 端 (如果可以的話),或是 client 端寫個小小的 wrapper function 將條件封裝起來即可。
    #include<boost/optional.hpp>
    
    boost::optional<double> my_sqrt(double)
    {
       return boost::optional<double>(value>=0, sqrt(value)); 
    }
    
    void Foo( double value)
    {
       boost::optional<double> sqrt_value = my_sqrt( value );
       if(sqrt_value)
       {
          cout << *sqrt_value << endl;
       }
       else
       {
          cout << "value should not be negative." << endl;
       }
    }

    接著我們用 BOOST_AUTO 簡化一下程式碼,最後 client 就只要記得檢查 optional 是否有效,而不必知道如何檢查。
    #include<boost/optional.hpp>
    
    using namespace boost;
    
    optional<double> my_sqrt(double)
    {
       return optional<double>(value>=0, sqrt(value)); 
    }
    
    void Foo( double value)
    {
       BOOST_AUTO( sqrt_value, my_sqrt( value ));
       if(sqrt_value)
       {
          cout << *sqrt_value << endl;
       }
       else
       {
          cout << "value should not be negative." << endl;
       }
    }

    2. assign
    有時我們需要對 STL container 大量的塞一些常數性質的資料,像是 error code 或是 event messages 之類的,以往就是在程式裡寫一堆 push_back 或 insert。
    vector<int> v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(4);
    v.push_back(7);
    v.push_back(9*9);
    
    map<int, string> m;
    m.insert ( make_pair ( 1, "one") );
    m.insert ( make_pair ( 2, "two") );
    m.insert ( make_pair ( 3, "three") );
    

    Boost 的 assign 函式庫實作了 "operator+="、逗號 "," 及 括號 "operator( )" 來簡化這些操作,下例結果同上。
    #include<boost/assign.hpp>
    using namespace boost::assign;
    
    vector<int> v;
    v += 1, 3, 4, 7, 9*9;
    
    map<int, string> m;
    m += make_pair ( 1, "one"), 
           make_pair ( 2, "two"), 
           make_pair ( 3, "three");
    
    

    使用 operator+= 在處理 map 時需要寫一大堆 make_pair,實在是很難看,我們可以用另一個 operator( ) 配合三個輔助函式insert( )、push_front( ) 及 push_back( ),更優雅地處理:
    #include<boost/assign.hpp>
    using namespace boost::assign;
    
    vector<int> v;
    push_back(v)(1)(3)(4)(7)(9*9);
    
    map<int, string> m;
    insert(m)( 1, "one")( 2, "two")( 3, "three");
    
    

    3. tribool
    一般我們在寫 C++ 要用到 Boolean 值時,通常有兩種型別可以用:BOOL 及 bool。由於早期的 C++ 並沒有 bool 這個關鍵字,所以各家 compiler 自行 typedef 或是 define 了 BOOL 這個型別 (或是 marco),通常它真正的型別是 int。例如微軟的 Windows API,而現代的 C++ (C99之後) 雖然加入了 bool 這個內建型別,但為了向下相容,Windows API 還是以 BOOL 來表示 Boolean 值。無論如何,現在應該儘量少用 BOOL 這個過時又不安全的東西了( 因為 BOOL 的值可能不為 TRUE 也不為 FALSE)。
    言歸正傳,bool 的 domain 應該只有 true 跟 false,這是廢話,但也因為這樣,我們無法用 bool 表示一個不確定的狀態。例如我們設一個 function 叫作 IsPrinterExists( ),用來偵測目前有沒有印表機存在,原型如下:
    bool IsPrinterExists();
    
    我們預期 IsPrinterExists( ) 應該回傳 true 表示有印表機,false 表示沒有,但如果偵測過程中有錯誤,那不管它回傳 true 或 false 都是不正確的(雖然設計上我們通常會當作 false 來處理,實務上大多也正確),應該要是一個 unknown 的值。
    Boost 引入一個新的型別,名為 tribool,它與 bool 差不多,但除了 true 跟 false 外還多了一個狀態 indeterminate。tribool 的 compare operation 有下列規則:
    • indeterminate 不是 true 也不是 false。
    • 任何與 indeterminate 比較的結果都是 indeterminate。
    • 對 || operator,只有與 true 的運算結果為 true,其它則為 indeterminate。
    • 對 && operator,只有與 false 的運算結果為 false,其它則為 indeterminate。
    • indeterminate 的 ! 運算結果還是 indeterminate。 
    • 可以使用 indeterminate( ) 來判斷是否為 indeterminate。 
    #include<boost/logic/tribool.hpp>
    
    using namespace boost;
    
    tribool tb(true);  // true
    tribool tb2(!tb)   // false
    
    if(tb)
    {
       // go here
       
       tb2 = indeterminate; // tb2 is indeterminate
       
       // OK, compare with indeterminate
       if( tb2 == indeterminate)
       {
          // go here
           cout << tb2 || true; // true
           cout << tb2 && false; // false
       }
       
       if(tb2)
           cout << "never printed";  // tb2 is not ture
       if(!tb2)
           cout << "never printed"; // tb2 is not false
       
       if( indeterminate(tb2) )
           cout << "indeterminate"; // yes
    }
    
    

    2011年5月6日 星期五

    C++0x 及 Boost 的自動型別推導:auto、decltype 及 BOOST_AUTO、BOOST_TYPEOF

    C++是個 static strong typing (靜態強型別) 的語言,因此所有變數在使用前都必須明確宣告其型別,這帶給了 C++ 執行快速的優點,並俱有較高的安全性。然而隨著 STL 及 自訂型別的大量使用,也帶來了一些寫程式上的小問題。

    舉例來說,我們常常要為 STL container 宣告 iterator 時,往往會寫這樣子的程式碼:

    std::map<int, std::string> myMap;
    ...
    ...
    std::map<int, std::string>::iterator itor = myMap.begin();
    // do something with itor

    其中我們的變數名稱只有四個字,而型別名稱卻有三十多字,若我們有許多這樣子的 container 及宣告,那程式碼中必然充斥著滿滿的宣告,而降低可讀性。對於這種問題,傳統的做法就是使用 typedef 或是 marco,但都只能解決一小部分的問題,就是程式碼看起來比較乾淨,但對於程式員而言,還是需要記得這冗長的型別名稱。myMap.begin( ) 回傳值的型別,你知我知 Compiler 也知,甚至在編譯期就可以確定了,那我們能夠少打這些多餘的字嗎?


    C++0x 提出了兩個關鍵字來解決這問題:auto 及 decltype。

    • auto 是個從 C 語言繼承而來的關鍵字,在 C++0x 之前,auto 是一種 storage-class specifiers,用來修飾變數宣告的 scope、lifetime 及 storage。如果一個變數的宣告前加上 auto 的話,那就表示是宣告一個 Local variable。如果一個變數宣告沒有加任何的 storage-class specifiers (如 static、register、extern …等),那預設就是auto。因為如此,auto 很少被使用。
      // Variable i is explicitly declared auto (local variable).
      auto int i = 0;
      // Variable j is implicitly declared auto (local variable).
      int j = 0; 
      在 VC2010 及 C++0x 中,auto 有了新的作用,用來表示一個經由 compiler 推導的型別 (Deduce Variable Type)。你可以在變數宣告前加上 auto 並給予初始值,該變數的型別會根據初始值來決定。可以被推導的型別有:const、volatile、pointer、reference 及 rvalue reference。
      我們可以在compile時加上參數。以選擇我們對於 auto 的使用方式:
      • /Zc:auto- : 表示將auto當成舊式的 storage-class specifiers
      • /Zc:auto (default): 表示將auto當成新式的 deduce variable type 

      // 宣告
      // Variable j is explicitly type int.
      int j = 0;
      // Variable k is implicitly type int because 0 is an integer.
      auto k = 0; 
      
      
      // 宣告 iterator (由其在loop裡)
      deque<double> dq1(2,0.1);
      
      for (auto it = dq1.begin(); it != dq1.end(); ++it)
      {  
          // do something
      }
      
      for_each(auto elem in dq1)
      {  
          // do something
      }
      
         
      // 宣告 pointer
      double x = 12.34;
      auto *y = new auto(x), **z = new auto(&x);
      
      
      // More
      auto x = 1, *y = &x, **z = &y; // Resolves to int.
      auto a(2.01), *b (&a);         // Resolves to double.
      auto c = 'a', *d(&c);          // Resolves to char.
      auto m = 1, &n = m;            // Resolves to int.
      
      
      // 搭配 :? 使用
      int v1 = 100, v2 = 200;
      auto x = v1 > v2 ? v1 : v2;
      
      
      // 搭配 const 使用
      int f(int x) { return x; }
      
      int main()
      {
          auto x = f(0);
          const auto & y = f(1);
          int (*p)(int x);
          p = f;
          auto fp = p;
          ...
      } 

      auto 雖然好用,但它並非萬能,以下為一些誤用 auto 的情況 (以下皆使用新式 auto,即 Compile with /Zc:auto )
      • 不能跟其它的 type-specifier 一起使用
       // error
      auto int x;
      • 用 auto 宣告變數必需給初始值
       // error
      auto x1;                 
      auto y1, y2, y3;         
      auto z1 = 1, z2, z3 = -1; 
      
      • auto 不可當作函式回傳型別, 亦不可當作 array 型別
       // error
      auto f( ){ }
      auto a[5];           
      auto b[1][2];        
      auto y[5] = x;       
      auto z[] = {1, 2, 3}; 
      auto w[] = x; 
      
      • auto 不可當作參數, 或 template 參數的型別
       // error
      void f(auto j){}
      
      template<class T> class C{};
      int main()
      {
         C<auto> c;   // C3539
         return 0;
      }
      
      • auto 搭配 new 使用時必需有初始值
       // error
      new auto();         
      auto x = new auto(); 
      
      • auto 不可被推導成 void, 亦不可宣告一個 auto pointer, 卻代入一物件(或內建型別)當初始值
       // error
      void f(){}
      auto x = f();
      
      auto* x = 123.0;
      
      class A { };
      A x;
      auto *p = x; 
      
      • 不可以該 auto 變數來自我初始化
      // error
      auto a = a;   
      auto b = &b;   
      auto c = c + 1; 
      auto* d = &d;  
      auto& e = e; 
      
      • 不可將其它型別轉型(cast)成 auto 型別
       // error
      auto(value);                      
      (auto)value;                     
      auto x1 = auto(value);            
      auto x2 = (auto)value;          
      auto x3 = static_cast<auto>(value); 
      
      • 在同一行所宣告的 auto 變數, 都必需被推導成同一型別
      // error
      // Variable x1 is a pointer to char, 
      // but y1 is a double.
      auto * x1 = "a", y1 = 3.14; 
      
      // Variable c is a char, 
      // but c1, c2, and c3 are pointers to pointers.
      auto c = 'a', *c1 = &c, * c2 = &c1, * c3 = &c2; 
      
      // Variable x2 is an int, 
      // but y2 is a double and z is a char.
      auto x2(1), y2(0.0), z = 'a'; 
      
      // Variable a is a pointer to int, 
      // but b is a pointer to double.
      auto *a = new auto(1), *b = new auto(2.0);
      
      • 不可對 auto 使用 sizeof 或 typeid
      auto x = 123;
      sizeof(x);    // OK
      sizeof(auto); // Error
      
      auto x = 123;
      typeid(x);    // OK
      typeid(auto); // Error
      

      • decltype 是 C++0x 新增的一個關鍵字,它接受一個 expression,並由該 expression 推導出一個型別。
      const int&& foo();
      int i;
      struct A { double x; };
      const A* a = new A();
      
      decltype(foo()) x1; // type is const int&&
      decltype(i) x2; // type is int
      decltype(a->x) x3; // type is double
      decltype((a->x)) x4; // type is const double&
      

      如今,auto 及 decltype 幾乎完全確定會制定於 C++0x當中了,但也是未來的 compiler 才會支援。這麼好用的東西,現在不能用實在是很可惜,所以 Boost 提供了兩個 marco 來模擬 auto 及 decltype。它們分別是BOOST_AUTO 及 BOOST_TYPEOF。
      • BOOST_AUTO 的用法為 BOOST_AUTO( Var, Expr),它會依 Expr 推導的型別宣告一個名為 Var 的變數,並初始化為 Expr 的值。
      • BOOST_TYPEOF 的用法為 BOOST_TYPEOF( Expr ),結果為一個經由推導 Expr 而成的型別指示詞 (Type specifier)。
      #include <boost/typeof/typeof.hpp>
      
      vector<string> func()
      {
          vector<string> v(10);
          return v;
      }
      
      int main()
      {
          BOOST_TYPEOF(2.0*3) x = 2.0*3;
          BOOST_AUTO(y, 2.3) ;
      
          BOOST_AUTO(&a, new double[20]);
          BOOST_AUTO(p, make_pair(1, "string"));
          BOOST_AUTO(v, func() );
          
          return 0;
      }
      

      參考資料:
      http://msdn.microsoft.com/en-us/library/dd465215.aspx
      http://www.boost.org/doc/libs/1_46_1/doc/html/typeof/tuto.html

      2011年5月5日 星期四

      Boost 之 smart pointer

      C++最令人苦惱的地方,不外乎就是語言特性太複雜( OO 及 Generic Programming 等等),以及缺乏自動記憶體管理。通常在寫C++時,我們會使用 RAII (Resource Acquisition Is Initialization) 這個 idiom (慣用手法) 來管理記憶體,也就是在物件的 constructor 中申請資源,然後在 destructor 釋放。但這僅僅於物件是建立在 stack 中 (Local variable)可行,若物件是建立於 heap 中,則 destructor 永遠不會被呼叫,除非我們手動 delete 它。

      為了更方便開發者管理記憶體,smart pointer 一直是C++陣營一直在探討的議題之一。而 C++98 中提供一個 smart point:auto_ptr
      auto_ptr 的 constructor 接受一個由 new operator 或是 object factory 所建立出的指標為參數,擁有這個指標所指的物件,並透過 override operator* 及 operator-> 來使其用起來像是一個真的指標。在 auto_ptr 退出其 scope 後,destructor 自然會被呼叫,而釋於資源。

      auto_ptr 解決了記憶體管理的問題,但它還不夠好用,最大的問題就是它對物件所有權的管理策略屬於 Destructive Copy,也就是一次只能有一個 auto_ptr 持有一個物件,禁止共享。

      std::auto_ptr<int> ptr1 ( new int (2) );
      std::auto_ptr<int> ptr2;
      ptr2 = ptr1 ; // transfer ownership from ptr1 to ptr2
      

      Boost.smart_ptr 基於 auto_ptr 擴充了六種適合不同需求所用的 smart pointers:
      1. scoped_ptr:類似 auto_ptr,但物件所有權無法轉移,因此同 auto_ptr,也無法被放到 STL container 中。另外,正如其名,我們只能在某個 scope 中使用它。
      2. scoped_array:scoped_ptr 的 array 版本,即內部是使用 new [ ] 及 delete [ ] 而非 new 與 delete。
      3. shared_ptr:是最類似 pointer 的 smart pointer,也是 Boost 中最有價值,最有用的組件。它實作了 reference count 機制,因此可以任何地方被 copy 及 assign value,當 reference count 為 0 時才會 delete 它。另外,shared_ptr 可以被使用於 STL container 中。
      4. shared_array:shared_ptr 的 array 版本。
      5. weak_ptr:一個用來協助 shared_ptr 的 smart pointer,通常它與 shared_ptr 共享同一個物件,但不會增加 reference count,因為它沒有 override operator* 及 operator->,因此也不會使用它來操作物件。它主要提供 use_count( ) 及 expired( ) 來回報 shared_ptr 的 reference count 以及內部指標是否有效等資訊。
      6. intrusive_ptr:一種侵入式的 smart pointer,由於 Boost 目前已不建議使用,因此也不多作介紹。
      詳細使用方法可參考 http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/smart_ptr.htm

      如果可以,請多多使用 shared_ptr 及 shared_array

      2011年4月12日 星期二

      Visual C++ 2010 中無法使用 Fixedsys 字型的問題

      在使用Visual C++時, 我個人習慣把Output Window的字型設成Fixedsys, 可是這個字型在Visual C++ 2010中卻找不到因此沒辦法選. 這是因為Visual C++ 2010 在 UI 上使用了 WPF 技術, 而 WPF 沒辦法畫 (rendering) 出像 Fixedsys 這種 raster font (點陣字型).

      看了一下微軟的討論串 https://connect.microsoft.com/VisualStudio/feedback/details/544089/can-t-use-fixedsys-strange-rendering-engine , 雖然一堆人吵著要微軟處理這問題, 但微軟似乎不打算修這問題.

      好吧, 那我先用 Consolas 擋一下...

      2011年4月11日 星期一

      將 Windows 中的磁碟機代號顯示在磁碟機名稱之前

      1. 加一個registry key:
      Path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer
      Key: ShowDriveLettersFirst
      Value: 4 
      2. 重開機 (或重開explorer)

      2011年4月10日 星期日

      檔案同步工具

      為了同步桌機與筆電上的一些檔案,
      就找到了這個由微軟自己出的免費工具 SyncToy,
      試用了一下還蠻簡單的, 可以單向同步跟雙向同步, 可惜沒有中文UI.

      下載位址

      2010年8月31日 星期二

      Visual C++ 2008 LINK : fatal error LNK1000: Internal error during IncrBuildImage 問題

      我在用 Visual C++ 2008 在 build solution 時, 常常發生 LINK 錯誤 :
      fatal error LNK1000: Internal error during IncrBuildImage
      看起來就是Linker有問題, google一下發現這個是VC的問題,
      只要裝一個Hotfix就解決了. 按此下載修正檔