null 指標深度剖析

來源:互聯網
上載者:User
  1. 什麼是null 指標常量(null pointer constant)?

    [6.3.2.3-3] An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

    這裡告訴我們:0、0L、'/0'、3 - 3、0 * 17 (它們都是“integer constant expression”)以及 (void*)0 等都是null 指標常量(注意 (char*) 0 不叫null 指標常量,只是一個null 指標值)。至於系統選取哪種形式作為空白指標常量使用,則是實現相關的。一般的 C 系統選擇 (void*)0 或者 0 的居多(也有個別的選擇 0L);至於 C++ 系統,由於存在嚴格的類型轉化的要求,void* 不能象 C 中那樣自由轉換為其它指標類型,所以通常選 0 作為空白指標常量,而不選擇 (void*)0。

  2. 什麼是null 指標(null pointer)?

    [6.3.2.3-3] If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

    因此,如果 p 是一個指標變數,則 p = 0;、p = 0L;、p = '/0';、p = 3 - 3;、p = 0 * 17; 中的任何一種賦值操作之後(對於 C 來說還可以是 p = (void*)0;), p 都成為一個null 指標,由系統保證null 指標不指向任何實際的對象或者函數。反過來說,任何對象或者函數的地址都不可能是null 指標。

  3. 什麼是 NULL?

    [6.3.2.3-Footnote] The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant

    即 NULL 是一個標準規定的宏定義,用來表示null 指標常量。因此,除了上面的各種賦值方式之外,還可以用 p = NULL; 來使 p 成為一個null 指標。

  4. null 指標(null pointer)指向了記憶體的什麼地方(null 指標的內部實現)?

    標準並沒有對null 指標指向記憶體中的什麼地方這一個問題作出規定,也就是說用哪個具體的地址值(0x0 地址還是某一特定地址)表示null 指標取決於系統的實現。我們常見的null 指標一般指向 0 地址,即null 指標的內部用全 0 來表示(zero null pointer,零null 指標);也有一些系統用一些特殊的地址值或者特殊的方式表示null 指標(nonzero null pointer,非零null 指標),具體請參見 C FAQ。

    幸運的是,在實際編程中不需要瞭解在我們的系統上null 指標到底是一個 zero null pointer 還是 nonzero null pointer,我們只需要瞭解一個指標是否是null 指標就可以了——編譯器會自動實現其中的轉換,為我們屏蔽其中的實現細節。注意:不要把null 指標的內部表示等同於整數 0 的對象表示——如上所述,有時它們是不同的。

  5. 如何判斷一個指標是否是一個null 指標?

    這可以通過與null 指標常量或者其它的null 指標的比較來實現(注意與null 指標的內部表示無關)。例如,假設 p 是一個指標變數,q 是一個同類型的null 指標,要檢查 p 是否是一個null 指標,可以採用下列任意形式之一——它們在實現的功能上都是等價的,所不同的只是風格的差別。

    指標變數 p 是null 指標的判斷:
    if ( p == 0 )
    if ( p == '/0' )
    if ( p == 3 - 3 )
    if ( p == NULL )  /* 使用 NULL 必須包含相應的標準庫的標頭檔 */
    if ( NULL == p )
    if ( !p )
    if ( p == q )
    ...

    指標變數 p 不是null 指標的判斷:
    if ( p != 0 )
    if ( p != '/0' )
    if ( p != 3 - 3 )
    if ( p != NULL )  /* 使用 NULL 必須包含相應的標準庫的標頭檔 */
    if ( NULL != p )
    if ( p )
    if ( p != q )
    ...

  6. 可以用 memset 函數來得到一個null 指標嗎?

    這個問題等同於:如果 p 是一個指標變數,那麼

    memset( &p, 0, sizeof(p) ); 和 p = 0;

    是等價的嗎?

    答案是否定的,雖然在大多數系統上是等價的,但是因為有的系統存在著“非零null 指標” (nonzero null pointer),所以這時兩者不等價。由於這個原因,要注意當想將指標設定為空白指標的時候不應該使用 memset,而應該用null 指標常量或null 指標對指標變數賦值或者初始化的方法。

  7. 可以定義自己的 NULL 的實現嗎?兼答"NULL 的值可以是 1、2、3 等值嗎?"類似問題

    [7.1.3-2] If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

    NULL 是標準庫中的一個符合上述條件的 reserved identifier (保留標識符)。所以,如果包含了相應的標準標頭檔而引入了 NULL 的話,則再在程式中重新定義 NULL 為不同的內容是非法的,其行為是未定義的。也就是說,如果是符合標準的程式,其 NULL 的值只能是 0,不可能是除 0 之外的其它值,比如 1、2、3 等。

  8. malloc 函數在分配記憶體失敗時返回 0 還是 NULL?

    malloc 函數是標準 C 規定的庫函數。在標準中明確規定了在其記憶體配置失敗時返回的是一個 “null pointer”(null 指標):

    [7.20.3-1] If the space cannot be allocated, a null pointer is returned.

    對於null 指標值,一般的文檔(比如 man)中傾向於用 NULL 表示,而沒有直接說成 0。但是我們應該清楚:對於指標類型來說,返回 NULL 和 返回 0 是完全等價的,因為 NULL 和 0 都表示 “null pointer”(null 指標)。

References:

Null Pointers(http://www.c-faq.com/null/index.html) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.