In this section:
This error is generated when any of the following occur:
new and freed with free.malloc and freed with delete .
|
Some compilers allow you to mix new and delete with malloc and free, but this is not good programming practice and may affect portability.
The following code shows a typical example of allocating a block of memory with new and then freeing it with free, instead of delete.
/*
* File: alloc1.cpp
*/
#include <stdlib.h>
int main() {
char *a;
a = new char;
free(a);
return 0;
} |
[alloc1.cpp:10] **ALLOC_CONFLICT** >> free(a); Memory allocation conflict: a ---- Associated Common Weakness Enumerations ---- CWE-762: Mismatched memory management routines CWE-763: Release of invalid pointer or reference free() used to deallocate memory which was allocated using new a, allocated at: main()alloc1.cpp, 9 Stack trace where the error occurred: main()alloc1.cpp, 10 |
The following code shows another typical example of this type of error, allocating a block of memory with malloc and then freeing it with delete.
/*
* File: alloc2.cpp
*/
#include <stdlib.h>
int main() {
char *a;
a = (char *) malloc(1);
delete a;
return 0;
} |
[alloc2.cpp:10] **ALLOC_CONFLICT** >> delete a; Memory allocation conflict: a ---- Associated Common Weakness Enumerations ---- CWE-762: Mismatched memory management routines CWE-763: Release of invalid pointer or reference delete operator used to deallocate memory not allocated by new block allocated at: malloc()(interface) main()alloc2.cpp, 9 Stack trace where the error occurred: main()alloc2.cpp, 10 |
This type of error can be corrected by making sure that all your memory allocations match up.
The table below shows Common Weakness Enumerations associated with this error.
| CWE | Description |
|---|---|
| CWE-762 | Mismatched Memory Management Routines |
| CWE-763 | Release of Invalid Pointer or Reference |