Consequence | Destructors will not be called |
The calling function is allocating memory with operator new[] , but freeing it with the scalar operator delete .
This is an error. According to the C++ standard and the Microsoft Visual C++ implementation, the result of this inconsistent operation is undefined. In addition, this error can cause the following problems, among others:
- The constructors for the individual objects in the array are invoked, but the destructors are not invoked.
- Unexpected results are likely to occur, because the global (or class-specific) operator new and operator delete are not compatible with operator new[] and operator delete[] .
Because the result of this code is undefined, the precise consequences of this defect are difficult to predict. It can result in memory leaks in classes with destructors that deallocate memory, inconsistent behavior for classes with destructors that perform some semantically significant operation, or memory corruptions and crashes when operators have been overridden. In other cases, the mismatch may be unimportant, depending on the implementation of the compiler and its libraries. PREfast for Drivers cannot always distinguish among these situations.
This warning is often reported on character or wide-character arrays. In this case, unless the operators are being overridden, there may not be any significant consequences of the mismatch, but the code should still be fixed.
In general, when you allocate memory with array operator new[] , you should free it with array operator delete[] . When the underlying object in the array is a primitive type, such as int, float, enum, or pointer, there are no destructors that can be called and PREfast for Drivers reports Warning 6283 .
Example
The following code example elicits this warning:
C *pC = new C[arraySize];
delete pC;
The following code example avoids this warning:
C *pC = new C[arraySize];
delete[] pC;