久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 C/C++ 中檢查空指針

Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
本文介紹了在 C/C++ 中檢查空指針的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在最近的代碼審查中,一位貢獻(xiàn)者試圖強(qiáng)制以下列方式對(duì)指針執(zhí)行所有 NULL 檢查:

In a recent code review, a contributor is trying to enforce that all NULL checks on pointers be performed in the following manner:

int * some_ptr;
// ...
if (some_ptr == NULL)
{
    // Handle null-pointer error
}
else
{
    // Proceed
}

代替

int * some_ptr;
// ...
if (some_ptr)
{
    // Proceed
}
else
{
    // Handle null-pointer error
}

我同意他的方式更清楚一點(diǎn),因?yàn)樗鞔_表示確保此指針不為 NULL",但我會(huì)反駁說(shuō),任何正在處理此代碼的人都會(huì)理解使用指針if 語(yǔ)句中的變量隱式檢查 NULL.另外我覺(jué)得第二種方法引入同類錯(cuò)誤的可能性較小:

I agree that his way is a little more clear in the sense that it's explicitly saying "Make sure this pointer is not NULL", but I would counter that by saying that anyone who's working on this code would understand that using a pointer variable in an if statement is implicitly checking for NULL. Also I feel the second method has a smaller chance of introducing a bug of the ilk:

if (some_ptr = NULL)

查找和調(diào)試絕對(duì)是一件痛苦的事情.

which is just an absolute pain to find and debug.

您更喜歡哪種方式,為什么?

Which way do you prefer and why?

推薦答案

根據(jù)我的經(jīng)驗(yàn),if (ptr)if (!ptr) 形式的測(cè)試是首選.它們不依賴于符號(hào) NULL 的定義.他們不會(huì)暴露意外分配的機(jī)會(huì).它們清晰簡(jiǎn)潔.

In my experience, tests of the form if (ptr) or if (!ptr) are preferred. They do not depend on the definition of the symbol NULL. They do not expose the opportunity for the accidental assignment. And they are clear and succinct.

正如 SoapBox 在評(píng)論中指出的那樣,它們與 C++ 類(例如 auto_ptr)兼容,這些類是充當(dāng)指針的對(duì)象并提供到 bool 來(lái)啟用這個(gè)習(xí)語(yǔ).對(duì)于這些對(duì)象,與 NULL 的顯式比較必須調(diào)用到指針的轉(zhuǎn)換,這可能具有其他語(yǔ)義副作用,或者比 bool 的簡(jiǎn)單存在檢查更昂貴轉(zhuǎn)換意味著.

As SoapBox points out in a comment, they are compatible with C++ classes such as auto_ptr that are objects that act as pointers and which provide a conversion to bool to enable exactly this idiom. For these objects, an explicit comparison to NULL would have to invoke a conversion to pointer which may have other semantic side effects or be more expensive than the simple existence check that the bool conversion implies.

我更喜歡能說(shuō)明含義而沒(méi)有不需要的文本的代碼.if (ptr != NULL)if (ptr) 具有相同的含義,但代價(jià)是冗余的特異性.下一個(gè)合乎邏輯的事情是編寫(xiě) if ((ptr != NULL) == TRUE) 并且這種方式是瘋狂的.C語(yǔ)言很清楚,一個(gè)由ifwhile等測(cè)試的布爾值具有特定含義,非零值為真,零為假.冗余并沒(méi)有讓它更清楚.

I have a preference for code that says what it means without unneeded text. if (ptr != NULL) has the same meaning as if (ptr) but at the cost of redundant specificity. The next logical thing is to write if ((ptr != NULL) == TRUE) and that way lies madness. The C language is clear that a boolean tested by if, while or the like has a specific meaning of non-zero value is true and zero is false. Redundancy does not make it clearer.

這篇關(guān)于在 C/C++ 中檢查空指針的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
if (cin gt;gt; x) - Why can you use that condition?(if (cin x) - 為什么你可以使用那個(gè)條件?)
主站蜘蛛池模板: 中文字幕在线看 | 国产精品免费看 | 伊人免费视频 | 一级片久久久 | 日韩精品在线观看视频 | 亚洲激情在线 | 色婷婷香蕉在线一区二区 | 国产福利av| 中文字幕精品一区久久久久 | 国产日韩在线播放 | 欧美久久一区二区 | 欧美aaaaa| 免费观看黄色av | 国产成人免费在线观看 | 国产精品黄 | 一区在线视频 | 色www| 九月丁香婷婷 | 五月天激情国产综合婷婷婷 | 国产激情视频在线 | 免费网站黄色 | 亚洲精品在线视频观看 | 久久精品免费看 | 欧美在线一区二区三区 | 久久综合99 | 日韩高清在线观看 | 国产精品久久久久久久 | 久色成人| 精品成人av | 在线观看小视频 | 黄色免费大片 | 亚洲第一在线 | 在线观看黄色av | 不卡av在线播放 | 韩日一级片 | 99精品成人| 国产精品二区一区二区aⅴ污介绍 | 国产成人免费在线 | 国产精品久久久久久99 | 中文字字幕 | 国产欧美一区二区三区在线看蜜臀 |