Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MEM-free-struct-field

In this section:
Synopsis

A struct's field is deallocated, but is not dynamically allocated.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A struct's field is deallocated, but is not dynamically allocated. Regardless of whether a struct is allocated on the stack or on the heap, all non-dynamically allocated fields will be deallocated when the struct itself is deallocated (either through going out of scope or calling a function like free()). Explicitly freeing such fields might cause a crash, or corrupt surrounding memory. Incorrect use of free() might also corrupt stdlib's memory bookkeeping, affecting heap memory allocation.

Coding standards
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 test {
  int a[10];
};

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;
  free(t.a);
}