Pointers to functions

L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

When the code requires callback functions, function pointers are ideal.
Just think of how many different "sort" functions you'd have to write,
if sort() /didn't/ perform key comparison through a function pointer.
In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.




- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEiDl4agVFX4UWr64RAr8rAJ4xqIMB529bJ7oa8a6EjwPPZnQOLACffvVQ
z2FCJiZ0dum1DbgI+IgjHtI=
=Mwp0
-----END PGP SIGNATURE-----
 
V

Vladimir Oka

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

I suggest you study the `qsort()` (or `bsearch()`) function, especially
the bit where you can pass a pointer to your own comparison function.
 
S

Suman

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Try writing a sort algorithm that works on arbitrary data
types.
 
A

Allan M. Bruce

Christian Christmann said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Regards,
Chris

I have used them in an Artificial Neural Network implementation. ANNs
require an activation function to normalise the inputs to a given range. It
is desirable to have this definable depending on the problem so using
pointers to functions is applicable and indeed faster considering this
operation will be executed many times.

As for the code being harder to read, I think the declarations are confusing
at first but they are fine once you know how. Remember back to learning C,
I bet normal pointers were difficult to read too ;-)

Allan
 
R

Robert Latest

On Thu, 08 Jun 2006 16:50:00 -0600,
in Msg. said:
in which situations pointers to functions might be more
efficient/convenient than a direct function call?

You can't compare the two since they are completely different.
Essentially a function pointer's use is to pass it to a piece of
software you don't (and don't want to) control.

I'm currently working on a GUI application. Much of the coding amounts
to writing pages and pages and pages of so-called callback functions
that I never call directly but only pass to the underlying GUI engine as
pointers.

robert
 
O

osmium

Christian Christmann said:
in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Before you can stew about efficient and convenient, you must have something
that actually works.

Try to write a program that evaluates a definite integral for a generalized
function, f(x), without using pointers to functions.

Yes, it is indeed harder to read. This is a good place to use a typedef, it
adds a nice bit of clarity. For the problem I posed, I *think* this is
right:

typedef double (*PF)(double);

where PF is a *type*, a pointer to a function that has a certain
"signature". Note that many of the functions in <math.h> have that
signature.
 
K

Kenneth Brody

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Consider a "virtual machine" engine, which executes programs for a
virtual machine. (For example, emulating another CPU, or something
like today's Java runtimes.)

Imagine this:

switch(opcode)
{
case OPCODE_0: DoOpcode0(); break;
case OPCODE_1: DoOpcode1(); break;
case OPCODE_2: DoOpcode2(); break;
... 252 more "case" statements ...
case OPCODE_255: DoOpcode255(); break;
}

versus:

typedef void (*VoidFunc)(void);
VoidFunc OpcodeTable[] =
{ DoOpcode0, DoOpcode1, DoOpcode2, ... };
...

(OpcodeTable[opcode])();

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Christian Christmann wrote On 06/08/06 18:50,:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Efficiency depends on the implementation, and different
implementations have different characteristics.

As for convenience: A function pointer is appropriate
whenever you want to call a function whose identity is not
known at the time the code is being written. For example,
suppose you want to calculate some integrals. You *could*
write one function to integrate cos(x)/(x-1) and another
for atan((x-1)/(x+1)) and another for ... Or you could
write just one general-purpose integrator that accepts a
pointer to a function to evaluate an arbitrary integrand
at x. Which would you prefer?
 
G

Gordon Burditt

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

*If* you know what function is going to be called by name, there
should be no such situation. If it is more efficient to call a
function via a pointer, the compiler should make one and use it on
functions not called via pointers. Using a function pointer when
you know the name of the function to be called isn't more convenient.
In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

The situations where function pointers are used to advantage are
those where the function pointer value is not a known constant.
Consider, for example, the pointer to the comparison function passed
as an argument to qsort(). The name isn't known in advance and
there may be uses in using a different function for each invocation
of qsort(). Function pointers are also commonly used as arguments
to register "callback functions", which are then called at appropriate
times. Their names won't be known in advance.

Gordon L. Burditt
 
C

Chris Dollin

Christian said:
in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

You use pointers to functions as function variables: to pass
functions as arguments (eg comparisions to sort routines, equality
to table lookups, callbacks to event handlers) or to allow them
to be stored for use elsewhen (eg methods for hand-implemented OO,
buffer fill/empty for file handling).

Of course, you haven't shown us the example you've found. It might
be a dreadful example, or it might be an example to show how it
works but not why you'd use it, or you may have failed to
understand it -- we can't tell.

Here's a mission. Define a type File with operations read and write
and close on it:

int fileRead( File f, char *buffer, int count );
int fileWrite( File f, char *buffer, int count );
int close( File f );

such that I can create new File objects which can read from, or write
to:

- FILE* objects
- character buffers in memory
- other Files, [un]compressing as we go
- other Files, [de]crypting as we go
- existing C-style strings [not for writing]

and anywhere else that might make sense -- /without/ using function
pointers.
 
G

Giannis Papadopoulos

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Regards,
Chris

Suppose you need some kind of a registry for functions. You then pass to
this registry a string and it returns to you the function that is mapped
to the string.

Can you imagine a way to do it without function pointers?

Of course the function that returns a function pointer is a bit ugly...

--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
 
C

Christian Christmann

Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Regards,
Chris
 
S

Sjouke Burry

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In the example I've found so far, I see no advantage of
using pointers to functions. Rather the code is harder to
read.

Regards,
Chris
Attaching data to algorithm?
Like a struct with data for a piece of road and
a pointer to the road drawing routine.
 
D

Dann Corbit

Sjouke Burry said:
Attaching data to algorithm?
Like a struct with data for a piece of road and
a pointer to the road drawing routine.

How would you implement quicksort for arbitrary data types without using
callback functions?
 
N

Naresh

Christian said:
Hi,

in which situations pointers to functions might be more
efficient/convenient than a direct function call?

In embedded systems, upgradations and patch-abilty. And function
pointer are the only saviours!!
 

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
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top