Thread overview
C(/C++) interoperability
Mar 21, 2003
Matthew Wilson
Mar 21, 2003
Walter
Mar 21, 2003
Matthew Wilson
Mar 21, 2003
Walter
Mar 21, 2003
Russ Lewis
Mar 21, 2003
Ilya Minkov
March 21, 2003
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



March 21, 2003
"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;
};


March 21, 2003
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
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;
> };
>
>


March 21, 2003
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
> 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;
> > };
> >
> >
>
>


March 21, 2003
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))) ]


March 21, 2003
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.