Passing D arrays to a C interface

Daniel Keep daniel.keep.lists at gmail.com
Mon Nov 5 01:11:58 PST 2007



Simen Haugen wrote:
> I'm building a library that should be callable from C, but I'm having 
> problems with arrays.
> The D documentation says there are no equivalent for dynamic arrays in C, so 
> how can I pass D arrays to C? Is this also a problem with other languages 
> like Pascal? 

I wouldn't try passing D arrays directly between C and D.  You can,
however, break them apart so that

  some_func(array);

Becomes:

  some_func(array.ptr, array.length);

Which will pass a pointer and a length.

	-- Daniel

P.S.  Be mindful that the D garbage collector will free any memory it
can't find a pointer to, so don't pass C pointers to GC allocated memory
unless you keep a reference to it somewhere.



More information about the Digitalmars-d mailing list