dynamic binding

Discussion in 'C Programming' started by aarklon@gmail.com, Feb 19, 2008.

  1. Guest

    Hi all,

    recently a friend asked me is there any dynamic binding in C...??
    to which i answered AFAIK it is in C++ only,
    but he says it is valid in C.

    if dynamic can be implemented via function pointers in C ,
    can anyone give an example for dynamic binding in C...??
    , Feb 19, 2008
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hi all,
    >
    > recently a friend asked me is there any dynamic binding in C...??
    > to which i answered AFAIK it is in C++ only,
    > but he says it is valid in C.
    >
    > if dynamic can be implemented via function pointers in C ,
    > can anyone give an example for dynamic binding in C...??
    >


    typedef struct
    {
    void (*getxy)(void *ptr, double t, int *x, int *y);
    int (*getlength)(void *ptr);
    } LINE;

    typedef struct
    {
    int x1;
    int y1;
    int x2;
    int y2;
    } STRAIGHTLINE;

    typedef struct
    {
    int x1;
    int y1;
    int r;
    } CIRCLE;


    int sl_getlength(void *ptr)
    {
    STRAIGHTLINE *sl = ptr;
    return (int) sqrt( (sl->x1 - sl->x2)*(sl->x1-sl->x2) +( sl->y1-sl->y2) *
    (sl->y1-sl->y2));
    }

    int circ_getlength(void *ptr)
    {
    CIRCLE *c = ptr;
    return (int) (3.14 * c->r * c->r);
    }

    Now we give a little alien a path.

    First set up a LINE with function pointers. Then call a dynamic function.

    void alien_attack(LINE *path, void *ptr)
    {
    int Nsteps;
    int i;

    Nsteps = path->getlength(ptr);
    for(i=0;i<Nsteps;i++)
    {
    /* display alien following attackpath */
    }
    }


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 19, 2008
    #2
    1. Advertising

  3. writes:

    > Hi all,
    >
    > recently a friend asked me is there any dynamic binding in C...??
    > to which i answered AFAIK it is in C++ only,
    > but he says it is valid in C.


    This is a terminology nightmare since the phrase means different things
    to different people. If we copy the common C++ meaning (essentially
    virtual functions[1]) we can do it in C. The more flexible meaning (say
    the one used in CLOS) requires non-standard features.

    > if dynamic can be implemented via function pointers in C ,
    > can anyone give an example for dynamic binding in C...??


    The usual way to set it up is with a table of function pointers
    embedded in a structure. A "derived" type optionally adds data to the
    structure and the "constructor" stores new function pointers either in
    the old table or in a new one. If the old table is re-used some trick
    is needed to save the old function pointer so it can still be used.

    There is a lot of "boilerplate" to do this in C and I am reluctant to
    type it all out. Maybe someone has a real example in use?

    [1] I have seen this described as "static dynamic binding"!

    --
    Ben.
    Ben Bacarisse, Feb 19, 2008
    #3
  4. cr88192 Guest

    "Ben Bacarisse" <> wrote in message
    news:...
    > writes:
    >
    >> Hi all,
    >>
    >> recently a friend asked me is there any dynamic binding in C...??
    >> to which i answered AFAIK it is in C++ only,
    >> but he says it is valid in C.

    >
    > This is a terminology nightmare since the phrase means different things
    > to different people. If we copy the common C++ meaning (essentially
    > virtual functions[1]) we can do it in C. The more flexible meaning (say
    > the one used in CLOS) requires non-standard features.
    >


    or we could also say it is not possible...
    or that it is...

    the amazing power of terms...


    >> if dynamic can be implemented via function pointers in C ,
    >> can anyone give an example for dynamic binding in C...??

    >
    > The usual way to set it up is with a table of function pointers
    > embedded in a structure. A "derived" type optionally adds data to the
    > structure and the "constructor" stores new function pointers either in
    > the old table or in a new one. If the old table is re-used some trick
    > is needed to save the old function pointer so it can still be used.
    >
    > There is a lot of "boilerplate" to do this in C and I am reluctant to
    > type it all out. Maybe someone has a real example in use?
    >


    what about a variation of delegation instead?...
    you create a new struct with a pointer to an instance of the old struct.
    calls into the new struct are handled as such, failing this, they are sent
    to the old struct.

    of course, usually I wrap my methods with ordinary functions, making it a
    little easier to hide the internal machinery...


    > [1] I have seen this described as "static dynamic binding"!
    >
    > --
    > Ben.
    cr88192, Feb 19, 2008
    #4
  5. user923005 Guest

    On Feb 19, 12:39 am, wrote:
    > Hi all,
    >
    > recently a friend asked me is there any dynamic binding in C...??
    > to which i answered AFAIK it is in C++ only,
    >  but he says it is valid in C.
    >
    > if dynamic can be implemented via function pointers in C ,
    > can anyone give an example for dynamic binding in C...??


    It's not the same as C++, but you can do the rough equivalent.

    See:
    http://people.csail.mit.edu/heinz/dt/
    "Evaluation Machines" Link:
    http://people.csail.mit.edu/heinz/dt/node15.html

    Ernst Heinz uses replaceable chess evaluation functions, which depend
    upon the phase of the game (For instance, in the opening, the king
    stays on his home row and tries to hide in a corner under some pawns.
    But in the endgame, the king runs to the center of the board.)

    See:
    http://chess.about.com/library/weekly/aa03e17.htm
    http://temposchlucker.blogspot.com/2007/07/endgame-strategy-thinking-out-loud.html
    user923005, Feb 19, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan
    Replies:
    2
    Views:
    2,530
    Jordan
    Feb 10, 2004
  2. Amit
    Replies:
    6
    Views:
    13,791
    Assimalyst
    Oct 24, 2006
  3. Replies:
    2
    Views:
    857
    Kevin Grover
    Oct 20, 2006
  4. JcFx
    Replies:
    0
    Views:
    276
  5. Vivek Nallur

    value binding and function binding

    Vivek Nallur, Sep 25, 2003, in forum: Ruby
    Replies:
    0
    Views:
    136
    Vivek Nallur
    Sep 25, 2003
Loading...

Share This Page