ES.61:使用delete[]销毁数组,使用delete销毁对象
That's what the language requires and mistakes can lead to resource release errors and/or memory corruption.
这是C++语言的要求,如果用错会导致资源释放错误或者内存破坏。
Example, bad(反面示例)
void f(int n)
{
auto p = new X[n]; // n default constructed Xs
// ...
delete p; // error: just delete the object p, rather than delete the array p[]
}
This example not only violates the no naked new rule as in the previous example, it has many more problems.
示例代码不仅违反了前面示例中的禁止暴露的new规则,还有更多其他错误。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es61-delete-arrays-using-delete-and-non-arrays-using-delete