[vlc-devel] Callbacks in modules written in C++ have wrong linkage

Filip Roséen filip at atch.se
Mon Feb 22 20:59:23 CET 2016


> Yes and no. ISO C leaves it as a portability issue what conversion what function and
> void pointers do, but somewhat recommends supporting it for debugging purpose anyway.

I did not know that; what revision of the standard and which section? I am happy
to learn new stuff.

> Win32 & OpenGL use a different style:
> void (*)() instead of void *.

The ISO C++ Standard (N3797), and I really thought ISO C did the same, considers
this to be *undefined-behavior* and have no clause related to debugging
purposes.

Using `void(*)()` instead of `void*` is in my book far better because of two
main reasons:

  1. An implementation is allowed to store code and memory in completely
     different manners, as such there is nothing saying that a
     *pointer-to-function* and *pointer-to-void* are compatible.

  2. The likelyhood of different types of function-pointers having the same size
     is far greater than `sizeof(void*) == sizeof(void(*)())`.
 
> I think that is more dangerous, as it allows accidental function calls without proper
> cast. Such a function call would fail on LP64 and LLP64  platforms due to default
> promotion rules.

As a note on the above, one can of course use a *pointer-to-function* type that
takes at least one argument of a type that is incompatible with everything else,
having a compile-time diagnostic if such function tries to be invoked.

------------------------------------------------------------------------------

On 16/02/22 21:39, Rémi Denis-Courmont wrote:

> Le 2016-02-22 14:37, Filip Roséen a écrit :
> >>NOTE
> >>
> >>Related to this problem is the fact that loading of modules stores
> >>pointer-to-functions inside pointer-to-void, which is
> >>undefined-behavior according to both the C and C++ Standard.
> 
> Yes and no. ISO C leaves it as a portability issue what conversion what function and
> void pointers do, but somewhat recommends supporting it for debugging purpose anyway
> (J.5.7). POSIX makes it well defined so we do not care:
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html
> 
> Win32 & OpenGL use a different style:
> void (*)() instead of void *.
> 
> I think that is more dangerous, as it allows accidental function calls without proper
> cast. Such a function call would fail on LP64 and LLP64  platforms due to default
> promotion rules.
> 
> -- 
> Rémi Denis-Courmont
> http://www.remlab.net/
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20160222/ffba4245/attachment.html>


More information about the vlc-devel mailing list