6278 - is allocated with array new [], but deleted with scalar delete

6278 - <Variable> is allocated with array new [], but deleted with scalar delete

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值