Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   dynamic binding (http://www.velocityreviews.com/forums/t592663-dynamic-binding.html)

aarklon@gmail.com 02-19-2008 08:39 AM

dynamic binding
 
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...??

Malcolm McLean 02-19-2008 11:55 AM

Re: dynamic binding
 

<aarklon@gmail.com> wrote in message
news:9382c71b-d16b-43c6-af71-3fe3e05f5737@e25g2000prg.googlegroups.com...
> 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


Ben Bacarisse 02-19-2008 12:12 PM

Re: dynamic binding
 
aarklon@gmail.com 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.

cr88192 02-19-2008 09:13 PM

Re: dynamic binding
 

"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:87ir0lrvpt.fsf@bsb.me.uk...
> aarklon@gmail.com 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.



user923005 02-19-2008 10:34 PM

Re: dynamic binding
 
On Feb 19, 12:39*am, aark...@gmail.com 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/2...-out-loud.html


All times are GMT. The time now is 04:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.