www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Passing D arrays to a C interface

reply "Simen Haugen" <simen norstat.no> writes:
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? 
Nov 05 2007
next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
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.
Nov 05 2007
prev sibling parent reply torhu <no spam.invalid> writes:
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? 
 

When you say 'pass D arrays to C', I assume you mean D code calling C functions? That would look like this: --- extern (C) void C_func_called_by_D(int* data, size_t length); void tryit() { int[] a = [1, 2, 3]; void C_func_called_by_D(a.ptr, a.length); } --- D dynamic arrays are not (portably) link-compatible with anything in C. Most of the time you would get away with pretending they are a 'struct dynarray { size_t len; void* data; };' But it's probably better to pass the size and the pointer separately, like in the example. If C code passes an array as a pointer and a length to D, you can turn it into a dynamic array with something like this: T[] toArray(T, U)(T* ptr, U len) { return ptr[0..len]; } Static (fixed-size) D arrays are compatible with C arrays when used as function arguments. They are passed as the address of the first element, just like in C. Be aware that this C prototype: void f(int a[]); is not compatible with this D prototype: extern (C) void f(int[] a); but with this one: extern (C) void f(int* a); But you have the length of the array in C: void f(float vec[3]); It's compatible with the same declaration in D. At least until they become pass-by-value in D2.0... I can't remember how arrays work in Pascal, sorry. :)
Nov 05 2007
parent reply David Brown <dlang davidb.org> writes:
On Mon, Nov 05, 2007 at 10:13:22AM +0100, torhu wrote:

 D dynamic arrays are not (portably) link-compatible with anything in C.  
 Most of the time you would get away with pretending they are a 'struct 
 dynarray { size_t len; void* data; };'  But it's probably better to pass 
 the size and the pointer separately, like in the example.

Especially best to not use this, since Walter seems to be thinking of changing this representation. Dave
Nov 05 2007
parent reply "Simen Haugen" <simen norstat.no> writes:
"David Brown" <dlang davidb.org> wrote in message 
news:mailman.0.1194283572.2338.digitalmars-d puremagic.com...
 On Mon, Nov 05, 2007 at 10:13:22AM +0100, torhu wrote:

 D dynamic arrays are not (portably) link-compatible with anything in C. 
 Most of the time you would get away with pretending they are a 'struct 
 dynarray { size_t len; void* data; };'  But it's probably better to pass 
 the size and the pointer separately, like in the example.

Especially best to not use this, since Walter seems to be thinking of changing this representation.

In v.1 too?!
Nov 06 2007
parent reply mwarning <nospam foo.bar> writes:
Simen Haugen Wrote:

 
 "David Brown" <dlang davidb.org> wrote in message 
 news:mailman.0.1194283572.2338.digitalmars-d puremagic.com...
 On Mon, Nov 05, 2007 at 10:13:22AM +0100, torhu wrote:


 Especially best to not use this, since Walter seems to be thinking of
 changing this representation.

In v.1 too?!

It may change in D2.0.
Nov 06 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
mwarning wrote:
 Simen Haugen Wrote:
 
 "David Brown" <dlang davidb.org> wrote in message 
 news:mailman.0.1194283572.2338.digitalmars-d puremagic.com...
 On Mon, Nov 05, 2007 at 10:13:22AM +0100, torhu wrote:


 Especially best to not use this, since Walter seems to be thinking of
 changing this representation.


It may change in D2.0.

AFAICT this wouldn't be a language change but an implementation change, so it could change in v1 as well.
Nov 08 2007