[cfe-dev] The %p conversion and cast to void*

David Chisnall David.Chisnall at cl.cam.ac.uk
Wed Feb 25 00:43:03 PST 2015


On 24 Feb 2015, at 22:38, Seth Cantrell <seth.cantrell at gmail.com> wrote:
> 
> Depends on how you define 'bug'. I include most kinds of undefined behavior, and if I came upon this warning in any of my code during everyday development I would fix it.

This is not undefined behaviour, it is implementation-defined behaviour, and it is implementation-defined behaviour that is shared across all vaguely modern C implementations, including some quite exotic ones.

The 'fix' for the warning clutters the code and makes it less readable.  People reading the code will wonder why you're casting the value to a pointer, when they thought it was already a pointer.

Having a warning if you pass a function pointer as the parameter for %p (which we currently lack) would be entirely sensible, as there *are* platforms with C implementations where data pointer and function pointer representations differ and the C standard makes no guarantee that these will work - though this warning might be silenced in POSIX mode, as POSIX relies on the ability to cast void* to a function pointer.  Having a warning for something that:

- Works on every current implementation
- Is used by so much code that no future implementation can safely break it

is a complete waste of everyone's time.

David





More information about the cfe-dev mailing list