Is void* compatible with function pointers?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 24 10:54:48 PDT 2014


On Tue, 24 Jun 2014 12:55:09 -0400, H. S. Teoh via Digitalmars-d  
<digitalmars-d at puremagic.com> wrote:

> On Tue, Jun 24, 2014 at 11:36:26AM -0400, Steven Schveighoffer via  
> Digitalmars-d wrote:
>> On Tue, 24 Jun 2014 00:24:50 -0400, deadalnix <deadalnix at gmail.com>  
>> wrote:
> [...]
>> >Anything that JIT is affected. Consider the pseudocode:
>> >
>> >void JIT() {
>> >     void* code = malloc(XXX);
>> >     // Write instructions into the allocated chunk of memory.
>> >
>> >     membar(); // Memory barrier to avoid reordering.
>> >
>> >     auto fun = cast(void function()) code;
>> >     fun(); // Even if your codegen is correct, anything can happen  
>> this point.
>> >}
>>
>> This is not what we are talking about.
>>
>> We are talking about whether a valid function pointer can be cast to
>> void * and back again. It is undefined for C/C++. Should it be defined
>> for D?
> [...]
>
> It's a judgment call. Defining it means that (void*).sizeof must be the
> size of the largest pointer type on a particular platform, meaning that
> if, for example, pointer to data is 32-bit but pointer to function is
> 64-bit, then we're effectively doubling the size of void* (it must be
> 64-bit to support free casting to/from function pointers) relative to
> any other non-function pointer. So code that deals only with data void*
> will have to pay for this cost for no benefit.

there are other options. We can not support the platform (I don't think  
this is a huge loss). We can not support casting function pointers to void  
* on that platform.

-Steve


More information about the Digitalmars-d mailing list