智能指针的使用及原理
智能指针的使用
内存泄露问题
内存泄露是指因为疏忽或错误,造成程序未能释放已经不再使用的内存的情况。比如:
int div()
{
int a, b;
cin >> a >> b;
if (b == 0)
throw invalid_argument("除0错误");
return a / b;
}
void func()
{
int* ptr = new int;
//...
cout << div() << endl;
//...
delete ptr;
}
int main()
{
try
{
func();
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
2025年3月28日大约 24 分钟