Compartilhar via


PREfast Warning 279 (Windows CE 5.0)

Send Feedback

279 - Allocation/free mismatch.
Additional Information: <variable> is allocated with scalar new, deleted with array delete [] (see allocation at <location>).

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.

This is undefined behavior according to the C++ standard and the Microsoft Visual C++ implementation.

This can cause problems for these reasons:

  • The constructors for the individual objects in the array are not invoked, but the destructors are invoked.
  • If global (or class-specific) operator new and operator delete are not compatible with operator new and operator delete, unexpected results are likely to occur
  • If memory is allocated with scalar new, it should typically be freed with scalar delete.

The results of this defect are difficult to predict. It can result in

  • Random behavior or crashes (because of usage of uninitialized memory because constructors are not invoked)
  • Memory allocations and crashes (if operators were overridden)

In rare cases, the mismatch can be unimportant. PREfast does not currently distinguish between these situations.

Example

Defective Source

C *pC = (C *)new byte[arraySize * sizeof (C)];

delete[] pC;

Corrected Source

C *pC = (C *)new C[arraySize];

delete[] pC;

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.