In this section:

Overview

ANSI C++ distinguishes between memory allocated with new and new[]. According to the standard, a delete call must match the new call. whether or not it has square brackets. 

Calling new[] and delete may cause the compiler to not call the destructor on each element of the array, which can lead to serious errors. Even worse, if the memory was allocated differently, memory may be corrupted. This is definitely poor practice and unlikely to work with future releases of the specific compiler. Insure++ distinguished between the following delete mismatches:


CodeDescriptionEnabledReportedPlatform
DELETE_MISMATCH

Mismatch between new/new[] and delete/delete[]

(error)RuntimeWindows/Unix
(bracket)

new, delete[]

(tick)RuntimeWindows/Unix
(nobracket)

new[], delete

(tick)RuntimeWindows/Unix


Some compilers allow you to mix new and delete with malloc and free, but this is not good programming practice and may affect portability.

Problem 1

The following code shows a block of memory allocated with new[] and freed with delete that has no square brackets.

/*
 * File: delmis1.cpp
 */

int main() {
	int *a = new int [5];
	delete a;
	return 0;
}

Diagnosis at Runtime

[delmis1.cpp:7] **DELETE_MISMATCH**
>> 		delete a;
Inconsistent usage of delete operator: a

---- Associated Common Weakness Enumerations ----
CWE-762: Mismatched memory management routines
CWE-763: Release of invalid pointer or reference

 array deleted without []
			a, allocated at:
				main() delmis1.cpp, 6
Stack trace where the error occurred:
				main() delmis1.cpp, 7
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the operator which doesn’t match.
  • Lines 5-7: CWEs associated with the problem.
  • Line 9: Brief description of the mismatch.
  • Line 12: Stack trace showing the function call sequence leading to the error.

Problem 2

The following code shows a block of memory allocated with new and no square brackets. The memory is freed with delete[]. This may cause some implementations of C++ to crash because the compiler may look for extra bits of information about how the block was allocated. Some compilers allow this type of error, extending the ANSI standard. In this case, there would be no extra bits, so the compiler would attempt to read from an invalid memory address.

/*
 * File: delmis2.cpp
 */

 int main() {
	int *a = new int;
	delete[] a;
	return 0;
}

Diagnosis at Runtime

[delmis2.cpp:7] **DELETE_MISMATCH**
>> 		delete[] a;
   Inconsistent usage of delete operator: a

---- Associated Common Weakness Enumerations ----
CWE-762: Mismatched memory management routines
CWE-763: Release of invalid pointer or reference

	[] used to delete a non-array
			a, allocated at:
				main() delmis2.cpp, 6

	Stack trace where the error occurred:
				main() delmis2.cpp, 7
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the operator which doesn’t match.
  • Lines 5-7: CWEs associated with the problem.
  • Line 9: Brief description of the mismatch.
  • Line 13: Stack trace showing the function call sequence leading to the error.

Repair

To eliminate this error, you need to change the delete call to match the new call. In our first example, this could be accomplished by calling delete[] instead of delete, and vice versa in the second example.

References

The table below shows Common Weakness Enumerations associated with this error.

CWEDescription
CWE-762Mismatched Memory Management Routines
CWE-763Release of Invalid Pointer or Reference
  • No labels