I\'ve read various posts on Stack Overflow RE: the derefercing type-punned pointer error. My understanding is that the error is essentially the compiler warning of the danger of
On top of what the other answers have said, this is a classic anti-pattern in C, and one which should be burned with fire. It appears in:
void *
(which doesn't suffer from this issue because it involves a value conversion instead of type punning), instead returning an error flag and storing the result via a pointer-to-pointer.For another example of (1), there was a longstanding infamous case in ffmpeg/libavcodec's av_free
function. I believe it was eventually fixed with a macro or some other trick, but I'm not sure.
For (2), both cudaMalloc and posix_memalign
are examples.
In neither case does the interface inherently require invalid usage, but it strongly encourages it, and admits correct usage only with an extra temporary object of type void *
that defeats the purpose of the free-and-null-out functionality, and makes allocation awkward.