MISRA C++:2023 Rule 8.2.5
Description
Rule Definition
reinterpret_cast shall not be used. 1
Rationale
The operator reinterpret_cast allows casting between unrelated
types, which can lead to undefined behavior. To avoid undefined behavior, do not use
reinterpret_cast.
As an exception, reinterpret_cast is allowed in these cases
because the behavior of these conversions are well-defined:
Casting a pointer to
T*, whereTis one ofvoid,char,unsigned char, orstd::byte, with cv qualification.Converting a pointer
pto an integer that is large enough to represent a pointer value, such asstd::uintptr_t.
Polyspace Implementation
Polyspace® reports a violation of this rule on use of the
reinterpret_cast operator. As exceptions, these uses are not
reported as violations:
reinterpret_cast<T*>whereTis one ofvoid,char,unsigned charorstd::byte.reinterpret_cast<T>(p)wherepis a pointer andTis an integer that is large enough to represent a pointer value.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
| Group: Expressions |
| Category: Required |
Version History
Introduced in R2024b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.