Is void* compatible with function pointers?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 23 13:49:27 PDT 2014


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.

Doing a bit of research, it seems that it is architecture dependent. For  
instance, a memory model may use pointers that are larger for function  
addresses than for object pointers. In this case, casting to and from void  
* would lose data.

But most platforms have the same pointer width for both data and  
functions. So it works, not by accident, but because that's what the  
architecture dictates.

I think it's important to state that the conversion from function pointer  
to void pointer is not specified as "undefined", but actually just not  
specified at all. I would say it's architecture-defined.

> 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.

-Steve


More information about the Digitalmars-d mailing list