Two ways of receiving arrays on the C ABI

Ali Çehreli acehreli at yahoo.com
Tue Oct 20 00:16:48 UTC 2020


On the D side, both of the following extern(C) functions take the same 
arguments.

1) func1 takes .length and .ptr implicitly as parts of a D array:

extern(C)
void func1(int[] arr) {
   assert(arr.equal(3.iota));
}

2) func2 takes .length and .ptr separately and then makes a slice 
explicitly:

extern(C)
void func2(size_t length, int * ptr) {
   auto arr = ptr[0..length];
   assert(arr.equal(3.iota));
}

C side declares and calls both of them the same way:

void func1(size_t length, int * ptr);
void func2(size_t length, int * ptr);

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

Everything works at least on Linux. Is this kosher, or am I using some 
internal knowledge?

Here is the ABI spec:

   https://dlang.org/spec/abi.html

One of the DConf Online 2020 slides thanks you! ;)

Ali


More information about the Digitalmars-d-learn mailing list