Skip to main content

IAR Embedded Workbench for RX 5.20

CERT-EXP36-C_a

In this section:
Synopsis

Do not cast pointers into more strictly aligned pointer types.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

Do not convert a pointer value to a pointer type that is more strictly aligned than the referenced type. Different alignments are possible for different types of objects. If the type-checking system is overridden by an explicit cast or the pointer is converted to a void pointer (void *) and then to a different type, the alignment of an object may be changed.

Coding standards
CERT EXP36-C

Do not convert pointers into more strictly aligned pointer types

Code examples

The following code example fails the check and will give a warning:

#include <assert.h>

void func(void) {
    char c = 'x';
    int *ip = (int *)&c; /* This can lose information */
    char *cp = (char *)ip;

    /* Will fail on some conforming implementations */
    assert(cp == &c);
}

The following code example passes the check and will not give a warning about this issue:

#include <assert.h>

void func(void) {
    char c = 'x';
    int i = c;
    int *ip = &i;

    assert(ip == &i);
}