Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.CAST.VOID_PTR_TO_OBJ_PTR.2012

Conversion performed from a pointer to void to a pointer to an object.

MISRA C 2012 Rule 11.5: A conversion should not be performed from pointer to void into pointer to object

C90 [Undefined 20], C99 [Undefined 22, 34]

Category: Advisory

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Rationale

Conversion of a pointer to void into a pointer to object may result in a pointer that is not correctly aligned, resulting in undefined behaviour. It should be avoided where possible but may be necessary, for example when dealing with memory allocation functions. If conversion from a pointer to object into a pointer to void is used, care should be taken to ensure that any pointers produced do not give rise to the undefined behaviour discussed under Rule 11.3.

Exception

A null pointer constant that has type pointer to void may be converted into pointer to object.

Example

uint32_t *p32;
void     *p;
uint16_t *p16;

p   = p32;                /* Compliant - pointer to uint32_t into
                           * pointer to void                       */
p16 = p;                  /* Non-compliant                         */
p   = ( void * ) p16;     /* Compliant                             */
p32 = ( uint32_t * ) p;   /* Non-compliant                         */