Skip to main content

IAR Embedded Workbench for RX 5.20

MEM-delete-op (C++ only)

In this section:
Synopsis

A memory location allocated with new [] is deleted with delete or free.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

A memory location allocated with the new [] operator is deleted with the delete operator. Use the delete [] operator instead. The consequence of using delete is that only the array element directly pointed to will be deallocated, as if it were allocated with the singular new operator. This will most likely cause a memory leak. If free is used the resulting behavior will be undefined, because there is no guarantee that new invokes malloc.

Coding standards
CWE 762

Mismatched Memory Management Routines

CWE 763

Release of Invalid Pointer or Reference

CWE 404

Improper Resource Shutdown or Release

Code examples

The following code example fails the check and will give a warning:

int main(void)
{
  int *p = new int[10];
  delete p; //should be delete[]

  return 0;
}

The following code example passes the check and will not give a warning about this issue:


int main(void)
{
  int *p = new int[10];
  delete [] p;

  return 0;
}