Rule Definition
The size of integer that is required when a pointer is converted to an integer is implementation-defined. Casting between a pointer and an integer type should be avoided where possible to prevent data loss, but maybe unavoidable when addressing memory-mapped registers or other hardware-specific features.
Note that C++ does not permit a pointer to be converted to any floating type.
Remediation
Do not cast a pointer type to an integer.
Note: When you have to necessarily do that ensure that the memory allocated to the int is sufficient
Violation Code Sample
struct S
{
int32_t i;
int32_t j;
};
void f ( S * s )
{
int32_t p = reinterpret_cast< int32_t >( s ); // Non-compliant
}
______________________________________________________
C:
int *p
Reference
Standards Reference:
MISRA C++ 2008, Rule 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
AUTOSAR C++, 2014, M5-2-9: A cast should not convert a pointer type to an integral type.
Related Technologies
Technical Criterion
CWE-704 - Incorrect Type Conversion or Cast
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.