Warning C6279
'variable-name' is allocated with scalar new, deleted with array delete []
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the scalar new
operator, but freed it with the array delete[]
operator. If memory is allocated with scalar new
, it should typically be freed with scalar delete
.
Remarks
The exact ramifications of this defect are difficult to predict. It might cause random behavior or crashes due to usage of uninitialized memory as constructors aren't invoked. Or, it might cause memory allocations and crashes in situations where operators have been overridden. The analysis tool doesn't currently distinguish between these situations.
To avoid these kinds of allocation problems altogether, use the mechanisms that are provided by the C++ Standard Library (STL). These include shared_ptr
, unique_ptr
, and containers such as vector
. For more information, see Smart pointers and C++ Standard Library.
Code analysis name: NEW_ARRAY_DELETE_MISMATCH
Example
The following code generates warning C6279. A
is allocated using new
but deleted using delete[]
:
class A
{
// members
};
void f()
{
A *pA = new A;
//code ...
delete[] pA;
}
The following code avoids this warning by using delete
instead:
class A
{
// members
};
void f()
{
A *pA = new A;
//code ...
delete pA;
}