TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OAL#007Checked automatically with code checker

Synopsis:Don't mix new and delete for array and non-array types
Language:C++
Severity Level:1
Category:Object Allocation


Description:

If you use "delete" on an array then only the first element will be deleted, thus creating a memory leak:

  void  foo() 
  {
    char* myString;
    myString = new char[10];
    delete myString;   // Wrong: memory leak introduced	
    delete[] myString; // Right
  }

The opposite could go wrong as well, i.e. applying "delete[]" to a non-array pointer:

  class X {   
    void  foo()
    {
      X* x = new X();
      delete[] x; // What happens here?
    }
  };

This causes undefined behavior in C++.



Literature References:
Ellemtel Rule 51
ISC++ Rule 8.2