Two ways of receiving arrays on the C ABI

Ali Çehreli acehreli at
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.
 > there are issues with structs. Not sure about length/ptr.

Thank you, Nic.

I see that doing the following is undefined behavior:

void dFunc(int[] arr) {                // <-- Takes slice

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



More information about the Digitalmars-d-learn mailing list