CERT-MEM34-C_b
In this section:
Synopsis
Only free memory allocated dynamically.
Enabled by default
Yes
Severity/Certainty
High/High

Full description
Freeing memory that is not allocated dynamically can result in heap corruption and other serious errors. This check is identical to MEM-free-field.
Coding standards
- CERT MEM34-C
Only free memory allocated dynamically
- CWE 590
Free of Memory not on the Heap
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
struct C{
int x;
};
int foo(struct C c) {
int *p = &c.x;
free(p);
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
struct C{
int *x;
};
int foo(struct C *c) {
int *p = (c->x);
free(p);
}