www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - The reason for SIGSEGV function pointer problem

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
OK, so I have narrowed down my SIGSEGV problem to having no real idea
how to do C function pointers in D code.

So I have a callback function that will be called from C library code.
It currently has signature:

    extern(C) int checkFrontend(void* _arguments, dvb_v5_fe_parms*
frontendParameters)
 
because of the extern(C) I believe you have to use a type alias in
order to specify the type in function definitions. Hence:

    alias check_frontend_t = extern(C) int function (void* args,
dvb_v5_fe_parms* parms);

In the constructor of an object to abstract the result of a call to the
C library code, the parameter is:

    check_frontend_t* cf

in the creation of the object using the constructor, I am using the
argument:

    &checkFrontend

However. This gives me a type error:

    extern (C) int function(void*, dvb_v5_fe_parms*)* cf

is not callable using argument type:

    extern (C) int function(void*, dvb_v5_fe_parms*)

all the other arguments/parameters types match exactly, this is the
only difference.

So why isn't &checkFrontend a thing of type check_frontend_t*?

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Jun 07
next sibling parent Paolo Invernizzi <paolo.invernizzi gmail.com> writes:
On Wednesday, 7 June 2017 at 16:50:26 UTC, Russel Winder wrote:

 In the constructor of an object to abstract the result of a 
 call to the C library code, the parameter is:

     check_frontend_t* cf
You should remove the pointer here... /Paolo
Jun 07
prev sibling next sibling parent ag0aep6g <anonymous example.com> writes:
On 06/07/2017 06:50 PM, Russel Winder via Digitalmars-d-learn wrote:
 So why isn't &checkFrontend a thing of type check_frontend_t*?
It's a thing of type `check_frontend_t`, which is a function pointer already. When you add an asterisk, you get a pointer to a function pointer.
Jun 07
prev sibling parent reply Mike Wey <mike-wey example.com> writes:
On 06/07/2017 06:50 PM, Russel Winder via Digitalmars-d-learn wrote:
 So why isn't &checkFrontend a thing of type check_frontend_t*
AFAIK, you would usually translate: typedef int (check_frontend_t*)(void *args, struct dvb_v5_fe_parms *parms); into: alias check_frontend_t = extern(C) int function (void* args, dvb_v5_fe_parms* parms); The problem there is that libdvdv5 defines it as (check_frontend_t) and not (check_frontend_t*). To get around that you can ommit the * in the declaration of dvb_scan_transponder, and then you should be able to pass &checkFrontend to it. -- Mike Wey
Jun 07
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Thanks also to Paolo Invernizzi=C2=A0and ag0aep6g for answering with a
similar response. Using Mike's response as it has extra detail.

On Wed, 2017-06-07 at 20:00 +0200, Mike Wey via Digitalmars-d-learn
wrote:
 On 06/07/2017 06:50 PM, Russel Winder via Digitalmars-d-learn wrote:
 So why isn't &checkFrontend a thing of type check_frontend_t*
=20 AFAIK, you would usually translate: =20 =20 typedef int (check_frontend_t*)(void *args, struct dvb_v5_fe_parms *parms);
The C code in dvb-scan.h is actually: typedef int (check_frontend_t)(void *args, struct dvb_v5_fe_parms *parms);
 into:
=20
 alias check_frontend_t =3D extern(C) int function (void* args,=C2=A0
 dvb_v5_fe_parms* parms);
I can't remember what DStep produced initially, but the above is what I have in dvb_scan.d. Per se it seems consistent, but then functions in D, and their signatures, may be totally different to functions in C.
 The problem there is that libdvdv5 defines it as (check_frontend_t)
 and=C2=A0
 not (check_frontend_t*).
 To get around that you can ommit the * in the declaration of=C2=A0
 dvb_scan_transponder, and then you should be able to pass
 &checkFrontend=C2=A0
 to it.
I am now at the stage of wondering if I remember C semantics, C++ semantics, and D semantics for "pointers to functions". There may also be an issue of there being a bug in DStep=E2=80=A6 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jun 08