Expressions and statements
ES.61
Delete arrays using `delete[]` and non-arrays using `delete`
Reason
That's what the language requires, and mismatches can lead to resource release errors and/or memory corruption.
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[]
}
Note
This example not only violates the no naked new rule as in the previous example, it has many more problems.
Enforcement
- Flag mismatched
newanddeleteif they are in the same scope. - Flag mismatched
newanddeleteif they are in a constructor/destructor pair.