这个非常简单的代码演示了复杂环境中野指针导致的那种麻烦。
int main()
{
int *a1 = new int;
int *tmp = a1;
delete a1;
// Now, the tmp pointer is a wild pointer, it's dangerous.
int *a2 = new int;
delete tmp;
// Now, the a2 pointer may be a wild pointer.
}
有什么方法可以检测或预防问题?智能指针在这里有帮助吗?