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

Source: Internet
Author: User
Tags scalar
6278-<variable> is allocated with array new [], but deleted with scalar Delete


Consequence Destructors will not be called

The calling function is allocating memoryOperator
New []
, But freeing it with the scalarOperator
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
    AndOperator
    Delete
    Are not compatibleOperator
    New []
    AndOperator
    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 uptions 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
Shoshould still be fixed.

In general, when you allocate memory with ArrayOperator
New []
, You shoshould free it with ArrayOperator Delete []
.
When the underlying object in the array is a primitive type, such
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.