Why is this an issue?
The size of integer required to hold a memory address is implementation-dependent. Therefore, casting a pointer (i.e. a memory address) to any
integral data type may result in data loss because the integral type is too small to hold the full address value.
When treating a memory address as integer type is absolutely required, you should be sure to use a large enough type to hold all the data.
Noncompliant code example
int *p;
int addr = ( int ) &p;
Resources
- MISRA C:2004, 11.3 - A cast should not be performed between a pointer type and an integral type.
- MISRA C++:2008, 5-2-9 - A cast should not convert a pointer type to an integral type.
- MISRA C:2012, 11.4 - A conversion should not be performed between a pointer to object and an integer type
- CERT, INT36-C. - Converting a pointer to integer or integer to pointer