dynamic binding

A

aarklon

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...??
 
M

Malcolm McLean

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 */
}
}
 
B

Ben Bacarisse

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"!
 
C

cr88192

Ben Bacarisse said:
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...

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"!
 
U

user923005

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top