![]() |
![]() |
0316 | ![]() |
||||
![]() | |||||||
| Pointers | REFERENCE - ISO:C90-6.3.4 Cast Operators - Semantics | ||||||
An expression of type 'pointer-to-void' is being cast to a 'pointer-to-object' type. 'void pointers' should be used with care. A conversion of this kind can only be performed safely if the pointer is correctly aligned for the type of object being pointed to. Notice that the 'void pointers' returned by library functions
malloc, calloc and realloc are guaranteed to be suitably aligned so that they may address an object of any type. Message 0316 is not generated when the value returned by these functions is cast.
For example:
/*PRQA S 702,1278,2017,2211,3120,3198,3199,3203,3227,3408,3602 ++*/
#include <stdlib.h>
extern void foo(unsigned char * puc)
{
void * pvd;
unsigned int * pui;
pui = (unsigned int *) puc; /* Messages 0310 and 3305 - dangerous */
pvd = (void *) puc; /* Message 0314 */
pvd = puc; /* Message 0315 */
pui = (unsigned int *) pvd; /* Message 0316 - dangerous */
pui = pvd; /* Message 0317 - dangerous */
pui = (unsigned int *) malloc(10u * sizeof(unsigned int)); /* */
pui = (unsigned int *) realloc(pui, 20u * sizeof(unsigned int)); /* Message 0315 */
free(pui); /* Message 0315 */
pui = (unsigned int *) calloc(10u, sizeof(unsigned int)); /* */
free(pui); /* Message 0315 */
}
MISRA-C:2004 Rules applicable to message 0316:
| Rule 11.4 (Advisory) | A cast should not be performed between a pointer to object type and a different pointer to object type. |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |