digitalmars.D - Passing D arrays to a C interface
- Simen Haugen (5/5) Nov 05 2007 I'm building a library that should be callable from C, but I'm having
- Daniel Keep (11/16) Nov 05 2007 I wouldn't try passing D arrays directly between C and D. You can,
- torhu (35/41) Nov 05 2007 When you say 'pass D arrays to C', I assume you mean D code calling C
- David Brown (4/8) Nov 05 2007 Especially best to not use this, since Walter seems to be thinking of
- Simen Haugen (3/10) Nov 06 2007 In v.1 too?!
- mwarning (4/13) Nov 06 2007 D1.0 doesn't get any changes, only bugfixes.
- Frits van Bommel (3/15) Nov 08 2007 AFAICT this wouldn't be a language change but an implementation change,
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
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
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
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
"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:In v.1 too?!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.
Nov 06 2007
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:D1.0 doesn't get any changes, only bugfixes. It may change in D2.0.Especially best to not use this, since Walter seems to be thinking of changing this representation.In v.1 too?!
Nov 06 2007
mwarning wrote:Simen Haugen Wrote:AFAICT this wouldn't be a language change but an implementation change, so it could change in v1 as well."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:D1.0 doesn't get any changes, only bugfixes. It may change in D2.0.Especially best to not use this, since Walter seems to be thinking of changing this representation.In v.1 too?!
Nov 08 2007