Closed brewmanz closed 7 years ago
The problem is innocent enough that I could include the fix, but it makes no sense on a standard point of view: in C99, void *
is implicitely convertible to any other pointer type. What compiler and build flags are you using here?
I'm actually compiling it as C++ (g++ under Ubuntu) for unit testing some C code I'm writing, so I'd like it to be bilingual i.e. work in C and C++. I've ended up creating my own .hpp headers from the .h ones (which are now left untouched). By using '#if __cplusplus' and 'extern "C"' the *.h can probably be made bilingual, and mman.c being compiled under C (gcc).
By using #if __cplusplus, I've got rid of the *.hpp I created. The downside is that I've had to shift some header-only code into mman.c e.g. array_length. (Maybe there's another way, but that was simplest for me).
Right. Moving the inline functions from the headers to the .c is probably the best of both worlds.
So it turns out that casting to void **
violates aliasing rules (since the original type can pretty much be any pointer type, not just the generic void *
). Using an union rather than casting avoids this problem and makes C++ happy at the same time.
I was getting
which I fixed by changing line
void **real_ptr = ptr;
tovoid **real_ptr = (void**)ptr;