Thread overview
Interfacing with C - div
Jul 05, 2004
Ben Hinkle
Jul 05, 2004
Ant
Jul 06, 2004
Walter
July 03, 2004
Hi,

I've been trying to do a simple C interface, but failed miserably until now:

extern (C) {
    struct div_t { int quot, rem; }
    div_t div(int numer, int denom);
}
int main() {
    div_t q = div(35, 10);
    printf("%d/%d\n", q.quot, q.rem);
    return 0;
}

Since my glibc doesn't have debuging symbols, the only useful thing that gdb reports is that it bailed out inside the div() call. Does someone know where did I fail?

My host is a Linux i686 2.6.6, glibc 2.3.2.

Regards,
Juliano.
July 05, 2004
Juliano Ravasi Ferraz wrote:

> Hi,
> 
> I've been trying to do a simple C interface, but failed miserably until now:
> 
> extern (C) {
>      struct div_t { int quot, rem; }
>      div_t div(int numer, int denom);
> }
> int main() {
>      div_t q = div(35, 10);
>      printf("%d/%d\n", q.quot, q.rem);
>      return 0;
> }
> 
> Since my glibc doesn't have debuging symbols, the only useful thing that gdb reports is that it bailed out inside the div() call. Does someone know where did I fail?
> 
> My host is a Linux i686 2.6.6, glibc 2.3.2.
> 
> Regards,
> Juliano.

I don't know why but it works for me when I add an extra int field to div_t:
 struct div_t { int quot, rem, foo; }
very odd.
-Ben
July 05, 2004
In article <ccbpb8$29d6$1@digitaldaemon.com>, Ben Hinkle says...
>
>Juliano Ravasi Ferraz wrote:
>
>> Hi,
>> 
>> I've been trying to do a simple C interface, but failed miserably until now:
>> 
>> extern (C) {
>>      struct div_t { int quot, rem; }
>>      div_t div(int numer, int denom);
>> }
>> int main() {
>>      div_t q = div(35, 10);
>>      printf("%d/%d\n", q.quot, q.rem);
>>      return 0;
>> }
>> 
>> Since my glibc doesn't have debuging symbols, the only useful thing that gdb reports is that it bailed out inside the div() call. Does someone know where did I fail?
>> 
>> My host is a Linux i686 2.6.6, glibc 2.3.2.
>> 
>> Regards,
>> Juliano.
>
>I don't know why but it works for me when I add an extra int field to div_t:
> struct div_t { int quot, rem, foo; }
>very odd.
>-Ben

oh,oh... this looks like the overload bug or the lookup rules in action. If I had phobos here I would check other declarations of div...

Ant


July 06, 2004
Ben Hinkle wrote:
> I don't know why but it works for me when I add an extra int field to div_t:
>  struct div_t { int quot, rem, foo; }
> very odd.

Wow! Really worked with a third int... sure it is odd...
Thanks!

Regards,
Juliano.
July 06, 2004
"Ben Hinkle" <bhinkle4@juno.com> wrote in message news:ccbpb8$29d6$1@digitaldaemon.com...
> Juliano Ravasi Ferraz wrote:
>
> > Hi,
> >
> > I've been trying to do a simple C interface, but failed miserably until now:
> >
> > extern (C) {
> >      struct div_t { int quot, rem; }
> >      div_t div(int numer, int denom);
> > }
> > int main() {
> >      div_t q = div(35, 10);
> >      printf("%d/%d\n", q.quot, q.rem);
> >      return 0;
> > }
> >
> > Since my glibc doesn't have debuging symbols, the only useful thing that gdb reports is that it bailed out inside the div() call. Does someone know where did I fail?
> >
> > My host is a Linux i686 2.6.6, glibc 2.3.2.
> >
> > Regards,
> > Juliano.
>
> I don't know why but it works for me when I add an extra int field to
div_t:
>  struct div_t { int quot, rem, foo; }
> very odd.

D returns structs that are 8 bytes long in EDX,EAX. Longer ones are returned on the stack. Perhaps gcc uses a different convention. An obj2asm on the code will tell the tale.