Exception Handling 新思維, Using Guards
Posted on August 31st, 2008 at 20:41 by fr3@K

續自 Exception Handling 新思維.

Preface

取材來說明如何以某種技巧寫出俱備特定目的的 code 有時不是一件容易的事. 我讀過好幾本這樣的 programming 書籍, 即便書中用了不小的篇幅來說明範例相關的 domain/business logic, 可是我依然無法感同身受 (當然也有可能是我太笨). 但當要說明的目的是 exception safety, 那就簡單多了. 以最多 C++ programmer 熟悉的 STL 為例, 應該不會錯到哪裡去.

本文選的題目是 STL 的 uninitialized_fill_n. 雖然這個題目可能同時也是不錯的 template programming 教材, 但是為了避免失焦, 這篇文字將假設讀者對文中使用到的 template programming 技巧以及 STL 有足夠的熟悉度, 並略過部份與 exception safety 無關的細節. 另外, 為方便排版, 文中 code snippet (代碼片斷) 可能不同於引用的來源, 兩者在文中的 context 應為等效.

這篇文字要從 library implementor 的角度出發, 拿 STL 當題材, 探討不同手法的 implementation 俱備的特性.
(more…)

del.icio.us:Exception Handling 新思維, Using Guards digg:Exception Handling 新思維, Using Guards spurl:Exception Handling 新思維, Using Guards newsvine:Exception Handling 新思維, Using Guards furl:Exception Handling 新思維, Using Guards Y!:Exception Handling 新思維, Using Guards 黑米共享書籤:Exception Handling 新思維, Using Guards 推推王:Exception Handling 新思維, Using Guards
Exception Handling 新思維
Posted on August 18th, 2008 at 0:38 by fr3@K

New mind set

一直以來 exception handling 與 exception safety 就是 C++ 最重要的課題之一. 正確的 exception handling 真的不是一件跟吃蛋糕一樣容易的事.

但, 它是否一定如此困難, 以致於 只有語言專家才能搞得定? 我認為這與事實相去甚遠.
(more…)

del.icio.us:Exception Handling 新思維 digg:Exception Handling 新思維 spurl:Exception Handling 新思維 newsvine:Exception Handling 新思維 furl:Exception Handling 新思維 Y!:Exception Handling 新思維 黑米共享書籤:Exception Handling 新思維 推推王:Exception Handling 新思維
Containers with Stateful Allocator, a Bug in C++98
Posted on June 8th, 2008 at 15:02 by fr3@K

前一陣子花了些時間寫一個 allocator library. 首要目標就是要能符合 C++ Standard 對 allocator 定義的 requirement. 當然也要相容於 Standard C++ Library 的 container 實作, 能與所有的 container function 正確的 inter-operate. 在這個過程中, 我意外地一腳踏進又一個 C++ 的陰暗角落.

先讓我把這個有點嚇人的結論寫在前面:

    你所使用的標準 container , 很可能無法完全正確地與 object-stateful 的 allocator 一起運作. 即便該 allocator 確實是符合標準規範的.

不過先別慌. 只要拿來與 container 搭配使用的是沒有 per instance 狀態的 allocator, 你就不會遭遇本文所討論的問題.

(more…)

del.icio.us:Containers with Stateful Allocator, a Bug in C++98 digg:Containers with Stateful Allocator, a Bug in C++98 spurl:Containers with Stateful Allocator, a Bug in C++98 newsvine:Containers with Stateful Allocator, a Bug in C++98 furl:Containers with Stateful Allocator, a Bug in C++98 Y!:Containers with Stateful Allocator, a Bug in C++98 黑米共享書籤:Containers with Stateful Allocator, a Bug in C++98 推推王:Containers with Stateful Allocator, a Bug in C++98
Why Const Matters
Posted on May 31st, 2008 at 16:51 by fr3@K

假設我們需要按照順序以紙條對 Alice 與 Bob 打招呼:

    void Alice(string& message_to_alice);
    void Bob(const string& message_to_bob);
    

(more…)

del.icio.us:Why Const Matters digg:Why Const Matters spurl:Why Const Matters newsvine:Why Const Matters furl:Why Const Matters Y!:Why Const Matters 黑米共享書籤:Why Const Matters 推推王:Why Const Matters
Strong Guarantee using Transaction
Posted on April 29th, 2008 at 23:24 by fr3@K

Abrahams Guarantees

在 C++ 的世界裡, 正確的 exception handling 是專業的 C++ programmer 不可或缺的技巧. 雖然它的概念並不困難, 但實作起來卻常不見得那麼容易.

要做到正確的 exception handling, 首先必須要了解什麼是 exception safety. 一個需要與 exception 打交道的 component 可在其介面實作人稱 Abrahams guarantees 的三種 exception safety 保證之一:

  1. The basic guarantee
  2. 允許操作失敗時改變物件的狀態, 但不能有 resource leak. 且該物件的狀態必須是可靠的仍然可以被解構, 操作失敗後該物件的狀態可以是不完全能被預測的.

  3. The strong guarantee
  4. 操作後的狀態只能是成功完成, 或是將該物件回復到操作之前的狀態並拋出一個 exception.

  5. The no-throw guarantee
  6. 操作不會拋出 exception.

(more…)

del.icio.us:Strong Guarantee using Transaction digg:Strong Guarantee using Transaction spurl:Strong Guarantee using Transaction newsvine:Strong Guarantee using Transaction furl:Strong Guarantee using Transaction Y!:Strong Guarantee using Transaction 黑米共享書籤:Strong Guarantee using Transaction 推推王:Strong Guarantee using Transaction
拒絕 Singleton
Posted on December 12th, 2007 at 1:32 by fr3@K

去年我寫過一篇文章, 大意是在說 大多數人所使用的 Singleton 實作都是有問題的. 在文章靠近後面的部份, 我也介紹了一種較少為人知, 我常用在 lifetime 是全局但 scope 是局部的 (global) 變數的 Singleton 實作. 雖然這實作離 fool proof 還差得遠, 在使用者能夠正確地 include header 的情況下, 它可能已經是最接近 bullet proof 的實作了.

“好吧, 那我就用類似你的 Singleton 實作總行了吧!?” 先別急著動手改 code, 實作是容易更換的, 但使用 Singleton Pattern 的原因卻與更重要的介面, 甚至架構的設計思維息息相關. 工作的實務經驗以及本身的興趣研究讓我體會到, 大多數的情況下 Singleton 的使用是可以避免的. 在這些非必要情況卻依然採用 Singleton 的原因常是:

  • 沒把問題想清楚
  • 好玩
  • 因為有個叫作 Singleton 的 pattern

(more…)

del.icio.us:拒絕 Singleton digg:拒絕 Singleton spurl:拒絕 Singleton newsvine:拒絕 Singleton furl:拒絕 Singleton Y!:拒絕 Singleton 黑米共享書籤:拒絕 Singleton 推推王:拒絕 Singleton
WIN32 的 _TCHAR 與 std::wstring 的問題
Posted on November 20th, 2007 at 3:28 by fr3@K

我知道很多人一直是這樣做的, 可是我從沒搞懂為什麼在 windows 平台上, 用上了 _TCHAR, _TEXT 再 define 個 _UNICODE. 或是用上了 whar_t 與 std::wstring. 就能算是 Unicode 化的程式?
(more…)

del.icio.us:WIN32 的 _TCHAR 與 std::wstring 的問題 digg:WIN32 的 _TCHAR 與 std::wstring 的問題 spurl:WIN32 的 _TCHAR 與 std::wstring 的問題 newsvine:WIN32 的 _TCHAR 與 std::wstring 的問題 furl:WIN32 的 _TCHAR 與 std::wstring 的問題 Y!:WIN32 的 _TCHAR 與 std::wstring 的問題 黑米共享書籤:WIN32 的 _TCHAR 與 std::wstring 的問題 推推王:WIN32 的 _TCHAR 與 std::wstring 的問題
NULL 指標兩三事
Posted on September 16th, 2007 at 21:51 by fr3@K

最近被我面試的十來位應徵者, 除了一位之外, 都不清楚以下兩個 pointer to NULL (指向 0 的指標) 的基本特性:

  • delete 一個指向 NULL 的指標是安全的
  • 以一個指向 NULL 的 C-style 字串為參數呼叫 strlen 會造成死機 (segmentation fault, access violation or whatever)

(more…)

del.icio.us:NULL 指標兩三事 digg:NULL 指標兩三事 spurl:NULL 指標兩三事 newsvine:NULL 指標兩三事 furl:NULL 指標兩三事 Y!:NULL 指標兩三事 黑米共享書籤:NULL 指標兩三事 推推王:NULL 指標兩三事
Resource Management using auto_ptr – Not Always So Simple
Posted on September 6th, 2007 at 23:03 by fr3@K

接續兩個多禮拜前, 那篇 囉唆的開場

話說那天面試 A 君作答最後一題的過程中, 或許是 A 君作答的速度慢了. 突然想到自己沒有用過真實世界的例子仔細檢驗過我所謂好方法. 面試後想像了幾個狀況, 我的 C++ 世界開始出現烏雲…
(more…)

del.icio.us:Resource Management using auto_ptr - Not Always So Simple digg:Resource Management using auto_ptr - Not Always So Simple spurl:Resource Management using auto_ptr - Not Always So Simple newsvine:Resource Management using auto_ptr - Not Always So Simple furl:Resource Management using auto_ptr - Not Always So Simple Y!:Resource Management using auto_ptr - Not Always So Simple 黑米共享書籤:Resource Management using auto_ptr - Not Always So Simple 推推王:Resource Management using auto_ptr - Not Always So Simple
Resource Management using auto_ptr – Intro
Posted on August 19th, 2007 at 2:14 by fr3@K

周五下午在新上任的工作進行了第一次面試. 被面試的這位應徵者 (就稱為 A 君吧) 已有三年以上在工作上使用 C++ 的經驗, 對自己的 C++ 能力的評比給的是熟悉 (還沒到精通) . 在聊了可能一個鐘頭左右後, 他表示自己寫程式很少發生 memory leak, 卻又舉例不出具體作法, 只表示這是經驗與習慣使然. 針對這點, 我在白板上出了個題目:

    class foo {};
    class bar
    {
      foo* fp_;
    public:
      bar(foo* fp)
      : fp_(fp)
      {
        // ....
      }
      ~bar()
      {
        delete fp_;
      }
    };
    
    bar* create_bar()
    {
      foo* fp = new foo;
      return new bar(fp);
    }
    

    假設你是負責維護 create_bar 的 programmer. 並且, 呼叫 create_bar 的使用者一定會記得 delete 掉被該 function new 出來的 instance . 請問 create_bar 這個函數有沒有潛在的 leak? 如果有, 可以如何改善?

(more…)

del.icio.us:Resource Management using auto_ptr - Intro digg:Resource Management using auto_ptr - Intro spurl:Resource Management using auto_ptr - Intro newsvine:Resource Management using auto_ptr - Intro furl:Resource Management using auto_ptr - Intro Y!:Resource Management using auto_ptr - Intro 黑米共享書籤:Resource Management using auto_ptr - Intro 推推王:Resource Management using auto_ptr - Intro

Page 1 of 3[ 1 ]23»