Converting an integer type to a pointer generally leads to unspecified behavior. There are several cases where it might be legitimate:
- Converting the integral literal
0
to the null pointer (but you should use nullptr
instead, see S4962),
- Converting back to a pointer a pointer value that was converted to a large enough integer (see S1767),
- On embedded devices, device drivers… converting a hard-coded address to a pointer to read some specific memory (this often goes together with
the use of
volatile
, since such memory values can change from the outside of the program).
Since even legitimate cases are corner cases that require to be reviewed carefully, this rule simply reports all places where an integer is cast
into a pointer (except the literal 0
).
Noncompliant code example
struct S {
int i;
int j;
};
void f(void* a);
void g(int i) {
S* s1 = (S*)i; // Noncompliant
f((void*)i); // Noncompliant
}