Insure++ Reference - DELETE_MISMATCH
The current version of ANSI C++ distinguishes between memory allocated
with new and new[] . A
delete call must (according to the standard)
match the new call, i.e. whether or not it has
[] .
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.
The following code shows a block of memory allocated with
new[] and freed with delete ,
without [] .
1: /*
2: * File: delmis1.C
3: */
4:
5: int main() {
6: int *a = new int [5];
7: delete a;
8: return 0;
9: }
1. [delmis1.C:7] **DELETE_MISMATCH**
>> delete a;
2. Inconsistent usage of delete operator: a
3. array deleted without []
a, allocated at:
main() delmis1.C, 6
4. Stack trace where the error occurred:
main() delmis1.C, 7
- Source line at which the problem was detected.
- Description of the problem and the operator which doesn't match.
- Brief description of the mismatch.
- Stack trace showing the function call sequence leading to the
error.
The following code shows a block of memory allocated with
new , without [] , and 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 do this to 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.
1: /*
2: * File: delmis2.C
3: */
4:
5: int main() {
6: int *a = new int;
7: delete[] a;
8: return 0;
9: }
1. [delmis2.C:7] **DELETE_MISMATCH**
>> delete[] a;
2. Inconsistent usage of delete operator: a
3. [] used to delete a non-array
a, allocated at:
main() delmis2.C, 6
4. Stack trace where the error occurred:
main() delmis2.C, 7
- Source line at which the problem was detected.
- Description of the problem and the operator which doesn't match.
- Brief description of the mismatch.
- Stack trace showing the function call sequence leading to the
error.
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.
DEAD_CODE
EXPR_BAD_RANGE
|