本篇內(nèi)容主要講解“C++捕捉執(zhí)行時錯誤怎么解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“C++捕捉執(zhí)行時錯誤怎么解決”吧!
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、豐縣網(wǎng)絡(luò)推廣、小程序制作、豐縣網(wǎng)絡(luò)營銷、豐縣企業(yè)策劃、豐縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供豐縣建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:bm7419.com
Avoid "mysterious" crashes. Avoid errors leading to (possibly unrecognized) wrong results.
避免“原因不明的”崩潰。避免導(dǎo)致(可能是沒有被認(rèn)識的)錯誤結(jié)果的問題。
Example(示例)
void increment1(int* p, int n) // bad: error-prone
{
for (int i = 0; i < n; ++i) ++p[i];
}
void use1(int m)
{
const int n = 10;
int a[n] = {};
// ...
increment1(a, m); // maybe typo, maybe m <= n is supposed
// but assume that m == 20
// ...
}
這里我們在use1中混入了一個將會引起數(shù)據(jù)破壞或者崩潰的小錯誤。指針/數(shù)量風(fēng)格的接口導(dǎo)致increment1()沒有辦法保護(hù)自己免受越界訪問的危害。如果我們檢查越界訪問的下標(biāo),那么錯誤直到訪問p[10]時才可能被檢出。我們可以修改代碼從而更早地進(jìn)行檢查。
void increment2(span<int> p)
{
for (int& x : p) ++x;
}
void use2(int m)
{
const int n = 10;
int a[n] = {};
// ...
increment2({a, m}); // maybe typo, maybe m <= n is supposed
// ...
}
Now, m <= n
can be checked at the point of call (early) rather than later. If all we had was a typo so that we meant to use n
as the bound, the code could be further simplified (eliminating the possibility of an error):
現(xiàn)在,m<=n可以在調(diào)用時更早的檢查。如果只是一個打字錯誤,我們本來是想用n作為邊界,代碼可以更簡單(排除錯誤的可能性):
void use3(int m){ const int n = 10; int a[n] = {}; // ... increment2(a); // the number of elements of a need not be repeated // ...}
Don't repeatedly check the same value. Don't pass structured data as strings:
不要重復(fù)檢查同一個值,不要將結(jié)構(gòu)化數(shù)據(jù)作為字符串傳遞。
Date read_date(istream& is); // read date from istream
Date extract_date(const string& s); // extract date from string
void user1(const string& date) // manipulate date
{
auto d = extract_date(date);
// ...
}
void user2()
{
Date d = read_date(cin);
// ...
user1(d.to_string());
// ...
}
The date is validated twice (by the Date
constructor) and passed as a character string (unstructured data).
date數(shù)據(jù)被兩次檢查(被Data類的構(gòu)造函數(shù))并且作為字符串傳遞(非構(gòu)造化數(shù)據(jù))
Example(示例)
過度檢查代價高昂。有些時候提早檢查會顯得很愚蠢:你可能永遠(yuǎn)都用不到那個值或者你可能只會用到數(shù)據(jù)的一部分,而這一部分又比全體更容易檢查。類似地,也不要增加可能改變接口的時間復(fù)雜度特性的有效性檢查(例如不要向一個時間復(fù)雜度為O(1)的接口給增加時間復(fù)雜度為O(n)的檢查)
class Jet { // Physics says: e * e < x * x + y * y + z * z
float x;
float y;
float z;
float e;
public:
Jet(float x, float y, float z, float e)
:x(x), y(y), z(z), e(e)
{
// Should I check here that the values are physically meaningful?
}
float m() const
{
// Should I handle the degenerate case here?
return sqrt(x * x + y * y + z * z - e * e);
}
???
};
由于測量誤差的存在,關(guān)于jet的物理定律(e * e < x * x + y * y + z * z
)不是恒定成立的。
示例代碼的情況中是否在構(gòu)造函數(shù)中增加檢查就是一個令人糾結(jié)的問題。
Enforcement(實施建議)
Look at pointers and arrays: Do range-checking early and not repeatedly 找到指針和數(shù)組,盡早進(jìn)行范圍檢查但不要重復(fù)檢查
Look at conversions: Eliminate or mark narrowing conversions 找到類型檢查,排除或確定進(jìn)行窄化轉(zhuǎn)換
Look for unchecked values coming from input 找到來此(外部)輸入的沒有經(jīng)過檢查的數(shù)據(jù)
Look for structured data (objects of classes with invariants) being converted into strings 找到被轉(zhuǎn)換為字符串的結(jié)構(gòu)化數(shù)據(jù)(包換約束條件的對象或類)
到此,相信大家對“C++捕捉執(zhí)行時錯誤怎么解決”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
名稱欄目:C++捕捉執(zhí)行時錯誤怎么解決
網(wǎng)頁網(wǎng)址:http://bm7419.com/article46/pscheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、網(wǎng)頁設(shè)計公司、網(wǎng)站設(shè)計、網(wǎng)站制作、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)