www.digitalmars.com         C & C++   DMDScript  

D - C(/C++) interoperability

reply "Matthew Wilson" <dmd synesis.com.au> writes:
I know that one can declare something extern (C) to use a function written
in C from within D source. I want to create a C function that can return
(D-type) char[].

Is this possible? I presume it'll require some C include files for the D
infrastructure ...

All help gratefully received.

Matthew
Mar 20 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Matthew Wilson" <dmd synesis.com.au> wrote in message
news:b5e2ar$2l47$1 digitaldaemon.com...
 I know that one can declare something extern (C) to use a function written
 in C from within D source. I want to create a C function that can return
 (D-type) char[].

 Is this possible? I presume it'll require some C include files for the D
 infrastructure ...

 All help gratefully received.

All you need to do is return a struct that looks like this: struct Array { int length; void *data; };
Mar 20 2003
next sibling parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
allocated from where?

"Walter" <walter digitalmars.com> wrote in message
news:b5eb13$2tuh$1 digitaldaemon.com...
 "Matthew Wilson" <dmd synesis.com.au> wrote in message
 news:b5e2ar$2l47$1 digitaldaemon.com...
 I know that one can declare something extern (C) to use a function


 in C from within D source. I want to create a C function that can return
 (D-type) char[].

 Is this possible? I presume it'll require some C include files for the D
 infrastructure ...

 All help gratefully received.

All you need to do is return a struct that looks like this: struct Array { int length; void *data; };

Mar 21 2003
parent "Walter" <walter digitalmars.com> writes:
It's returned in the register pair EDX:EAX, so it doesn't matter where it is
allocated.


"Matthew Wilson" <dmd synesis.com.au> wrote in message
news:b5ehu2$3kd$1 digitaldaemon.com...
 allocated from where?

 "Walter" <walter digitalmars.com> wrote in message
 news:b5eb13$2tuh$1 digitaldaemon.com...
 "Matthew Wilson" <dmd synesis.com.au> wrote in message
 news:b5e2ar$2l47$1 digitaldaemon.com...
 I know that one can declare something extern (C) to use a function


 in C from within D source. I want to create a C function that can



 (D-type) char[].

 Is this possible? I presume it'll require some C include files for the



 infrastructure ...

 All help gratefully received.

All you need to do is return a struct that looks like this: struct Array { int length; void *data; };


Mar 21 2003
prev sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Walter wrote:

 "Matthew Wilson" <dmd synesis.com.au> wrote in message
 news:b5e2ar$2l47$1 digitaldaemon.com...
 I know that one can declare something extern (C) to use a function written
 in C from within D source. I want to create a C function that can return
 (D-type) char[].

 Is this possible? I presume it'll require some C include files for the D
 infrastructure ...

 All help gratefully received.

All you need to do is return a struct that looks like this: struct Array { int length; void *data; };

Walter, I know that yo ucan do this, but it is a very bad idea! This struct is not defined in the spec, so it totally depends on the implementation of the compiler. We need to define a standard way to do this, even if the standard is simply this. -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Mar 21 2003
parent Ilya Minkov <midiclub 8ung.at> writes:
Russ Lewis wrote:
 Walter, I know that yo ucan do this, but it is a very bad idea!  This struct is
 not defined in the spec, so it totally depends on the implementation of the
 compiler.  We need to define a standard way to do this, even if the standard is
 simply this.

It's the same with "%.*s" printf format - it relies upon this struct just as well. So it can be considered as a part of specification. It is even mentioned under "memory layout" in the spec. -i.
Mar 21 2003