Memory accessed after deallocation
This defect occurs when you access a
block of memory after freeing the block using the free
function.
When a pointer is allocated dynamic memory with malloc,
calloc or realloc, it points to a memory
location on the heap. When you use the free function on this
pointer, the associated block of memory is freed for reallocation. Trying to access
this block of memory can result in unpredictable behavior or even a segmentation
fault.
The fix depends on the root cause of the defect. See if you intended to free the memory later or allocate another memory block to the pointer before access.
As a good practice, after you free a memory block, assign the corresponding pointer to NULL. Before dereferencing pointers, check them for NULL values and handle the error. In this way, you are protected against accessing a freed block.
| Group: Dynamic memory |
| Language: C | C++ |
| Default: On |
Command-Line Syntax: FREED_PTR |
| Impact: High |
| CWE ID: 416, 825 |