Is void* compatible with function pointers?

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 23 13:59:31 PDT 2014


On Mon, Jun 23, 2014 at 04:49:27PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> On Mon, 23 Jun 2014 16:30:41 -0400, Ali Çehreli <acehreli at yahoo.com> wrote:
> 
> >In C and C++, void* is for data pointers but it works (by accident?) for
> >function pointers on all popular platforms.

AFAIK, in C++ casting a function pointer to void* is undefined
behaviour, and while it does work for the most common platforms, there
are systems for which it would fail catastrophically. This is a
particularly nasty sticking point for Posix C++ implementations (you can
find discussions on this topic if you google for it), because according
to the spec, dlsym() should never work for function symbols in the
loaded library, though in practice, all Posix platforms (that I know of)
don't actually have any problem with it.


[...]
> >Does D have anything to say about this topic?
> 
> Since most architectures use same-size words for function addresses
> and object addresses, D would be fine to say it's defined and valid. I
> think the extreme outliers are architectures that are not equal, and D
> will not be harmed too badly by making this distinction. Any D flavor
> that would be ported to such an architecture may have to be a derived
> language.
[...]

We *could* technically define D's void* to be the same size as the
largest pointer type on that particular platform, and it would avoid the
problem. The issue in C++ arises because void* is assumed to point to
*data* as opposed to code, so it may be smaller than necessary to be a
function pointer.

But again, in practice I think it's safe to say that the platforms
affected by this issue are few in number and not commonly-used, so we
should be safe.


T

-- 
Study gravitation, it's a field with a lot of potential.


More information about the Digitalmars-d mailing list