檢測
檢測是編碼標準的補充。二者各有裨益,但結合使用效果特別好。機靈的 C 或 C++ 專業人員甚至可以瀏覽不熟悉的源代碼,并以極低的成本檢測內存問題。通過少量的實踐和適當的文本搜索,您能夠快速驗證平衡的 *alloc() 和 free() 或者 new 和 delete 的源主體。人工查看此類內容通常會出現像清單 7 中一樣的問題。
清單 7. 棘手的內存泄漏
static char *important_pointer = NULL;
void f9()
{
if (!important_pointer)
important_pointer = malloc(IMPORTANT_SIZE);
...
if (condition)
/* Ooops! We just lost the reference
important_pointer already held. */
important_pointer = malloc(DIFFERENT_SIZE);
...
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/