Passing static array to C variadic function

Daniel Gibson via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 22 01:13:00 PDT 2014


Am 21.07.2014 06:07, schrieb Daniel Murphy:
>
>
> "Daniel Gibson"  wrote in message news:lqh3vb$c2b$1 at digitalmars.com...
>
>> * passing stuff to the function is done as C expects it (not done,
>>    also: are there other cases than the static array one that are
>>    different?)
>
> Dynamic arrays.
>
> D used to allow passing static and dynamic arrays to C varargs with no
> problems.  This lead to nasty segfaults, especially with this code:
>
> printf("Hello %s\n", "segfault");

If the compiler did the right thing for extern (C) functions (i.e. 
implicitly passing "segfault" by reference), this shouldn't cause a 
segfault.

>
> which looks perfectly valid in D but certainly isn't.  What you actually
> meant to pass is up to you to specify.  It would not make a lot of sense
> for C functions to have a different set of implicit conversion rules to
> other functions.
>
>> * Disallowing template arguments (because how would I declare and call
>>    that function in C?) (not done)
>
> This is probably worth reporting, I doubt it works correctly.
>
>> * /Maybe/ disallowing types as arguments/return types that are not
>>    supported by C (D classes; not done)?
>>    (OTOH, one the C side one could just handle them as void* and pass
>>    instances around opaquely)
>
> Yes, this is why they are not disallowed.  This is just too damn useful.
>
> printf("Pointer: %p\n", new Class());

Well, that printf("%s", "asdf"); doesn't work (really?) and that I have 
to pass static arrays  arr.ptr in varargs, is much more 
surprising/unexpected than having to casts class objects to void* when 
passing them to a C function.

But still, it probably is useful to have type checking on the D side 
even when just treating them as void* on the C side.

Cheers,
Daniel



More information about the Digitalmars-d mailing list