Two ways of receiving arrays on the C ABI

Ali Çehreli acehreli at yahoo.com
Tue Oct 20 15:43:11 UTC 2020


On 10/19/20 6:28 PM, Nicholas Wilson wrote:> On Tuesday, 20 October 2020 
at 00:16:48 UTC, Ali Çehreli wrote:
 >> On the D side, both of the following extern(C) functions take the same
 >> arguments.
 >
 > https://github.com/dlang/dmd/pull/8120
 >
 > there are issues with structs. Not sure about length/ptr.

Thank you, Nic.

I see that doing the following is undefined behavior:

extern(C)
void dFunc(int[] arr) {                // <-- Takes slice
   assert(arr.equal(3.iota));
}

// A C function
void dFunc(size_t length, int * ptr);  // <-- Passes length+pointer

   int arr[] = { 0, 1, 2 };
   dFunc(3, arr);

C does not define an ABI. So, length+pointer can be passed in any way 
from there. The good thing is that D is aware of the "C Application 
Binary Interface of the target system"[1] but that means D would support 
taking length+pointer as well. Slice arguments are still strictly in 
length+pointer order for D.

So, it works on my Linux system by chance.

Ali

[1] https://dlang.org/spec/abi.html




More information about the Digitalmars-d-learn mailing list