PREfast Warning 283 (Windows CE 5.0)
283 - Allocation/free mismatch.
Additional Information: <variable> is allocated with array new [], deleted with scalar delete (see allocation at <location>).
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.
This is undefined behavior according to the C++ standard; however, when using default global operators, it behaves correctly with the Microsoft Visual C++ 6.0 and Visual C++ .NET 2002 implementations.
In addition to the inherent risk involved in relying on undefined behavior, if global (or class-specific) operator new and operator delete are not compatible with operator new and operator delete, unexpected results can occur.
The results of this defect are difficult to predict. It can result in
- Leaks
- Memory corruptions
- Crashes (if operators are overridden)
In most cases, the mismatch can be unimportant, depending on the implementation of the compiler and its libraries. PREfast cannot distinguish between these situations.
If memory is allocated with array new, it should typically be freed with array delete.
If the underlying object in the array is a class type, there is an additional complication: the destructors for the individual objects are not called. In this case, warning 278 is reported instead.
This message is often reported on character or wide-character arrays.
Example
Defective Source
char *str = new char[arraySize];
delete str;
Corrected Source
char *str = new char[arraySize];
delete[] str;
Send Feedback on this topic to the authors