MISRAC2012-Dir-4.13_h
In this section:
Synopsis
(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence. A struct field is deallocated without first having been allocated. This might cause a runtime error.
Enabled by default
No
Severity/Certainty
Medium/Medium

Full description
A struct field is deallocated without first having been allocated.
Coding standards
- MISRA C:2012 Dir-4.13
(Advisory) Functions which are designed to provide operations on a resource should be called in an appropriate sequence
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
struct test {
int *a;
};
void example(void) {
struct test t;
free(t.a);
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
struct test {
int *a;
};
void example(void) {
struct test t;
t.a = malloc(sizeof(int));
free(t.a);
}