The answer seems obvious, but considering this MISRA rule
Rule 11.5 A conversion should not be performed from pointer to void into pointer to object
would it not be safer to have memory allocation functions like "malloc" return a pointer to a char* instead? This type of pointer has no alignment issues.
Thanks.
A char* has no alignment issues. But that is irrelevant for all the times when you are allocating objects of a type that does have alignment issues.
Note that malloc() can't know what type you are allocating. And so can't know what the alignment requirements may be. So the only workable solution is for malloc() to always produce a pointer of the "best" possible alignment class that the processor might require. This means it's safe to type cast the returned pointer into a pointer to int or a pointer to double or whatever you need.
MISRA has to limit itself into what is possible by the programming language. And malloc() is part of the C language standard. But the goal with the MISRA rule isn't to point finger at malloc(), but to make sure that people don't try to send random void pointers as parameters or store in structs just to later decide what to recast the pointers into. So developers should avoid trying to implement abstract data types centered around "base class" code that operates on void pointers.