Callbacks in C

T

Tuxpal

What are the pros/cons of using Callback mechanism in C? (Design perspective)

What are the scenarios where it can be applied ?

Tuxpal
 
E

Emmanuel Delahaye

In said:
What are the pros/cons of using Callback mechanism in C? (Design
perspective)

pros : flexibility, reusability, modularity, testability.
cons : the produced code can be harder to read or to debug... specially when
several levels of callbacks are implied.
What are the scenarios where it can be applied ?

Mainly, software components (flexible libraries). Actually, it covers any
kind of applications... Sky is the limit.
 
D

Dan Pop

In said:
What are the pros/cons of using Callback mechanism in C? (Design perspective)

It's a design issue, not a language isse.
What are the scenarios where it can be applied ?

For starters, have a look at the specification of bsearch() and qsort()
from the standard C library.

Dan
 
M

Malcolm

Tuxpal said:
What are the pros/cons of using Callback mechanism in C?
(Design perspective)

What are the scenarios where it can be applied ?
Callbacks are usually used to leave some functionality out of a module, to
be provided by the caller. A standard library example is qsort(). Another
example would be if you implement some sort of database structure, and
require the caller to provide a getkey() function to retrieve the key from
the data.

The advantage is that the functions using callback are much more general.
qsort() for instance can sort any flat array without being recompiled.

One disadvantage is that providing the callback puts quite a burden on the
caller - you have to know exactly what the comparison function provided to
qsort() is expected to return, for example. If you accidentally return
order-dependent results for two items that in fact compare as equal then the
call could strangely fail just before shipping.

Another disadvantage is that it is harder to write a call-tree of a program
that uses function pointers. For short callbacks this isn't too much of a
problem, but if you use them too heavily you can no longer have a structured
program. This can be a difficulty in Windows programming, for example.
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top