Warning C6283
'variable-name' is allocated with array new [], but deleted with scalar delete
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the array new []
operator, but freed it with the scalar delete
operator.
Remarks
This defect might cause leaks, memory corruptions, and, in situations where operators have been overridden, crashes. If memory is allocated with array new []
, it should typically be freed with array delete[]
.
Warning C6283 only applies to arrays of primitive types such as integers or characters. If elements of the array are objects of class type then warning C6278 is issued.
The use of new
and delete
has many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of potential leaks 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: PRIMITIVE_ARRAY_NEW_DELETE_MISMATCH
Example
The following code generates warning C6283. str
is allocated using new ...[...]
but is freed using the mismatched function delete
:
void f( )
{
char *str = new char[50];
delete str;
}
The following code remediates this warning by using the matching function delete[]
:
void f( )
{
char *str = new char[50];
delete[] str;
}