This rule is Obsolete | |
Synopsis: | Don't mix new and delete for array and non-array types |
Language: | C++ |
Severity Level: | 1 |
Category: | Object Allocation |
Description: |
If you use "delete" on an array then only the first element will be deleted, thus creating a memory leak: void foo() { char* myString; myString = new char[10]; delete myString; // Wrong: memory leak introduced delete[] myString; // Right } The opposite could go wrong as well, i.e. applying "delete[]" to a non-array pointer: class X { void foo() { X* x = new X(); delete[] x; // What happens here? } }; This causes undefined behavior in C++. |