By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

Pointers to functions

P: n/a
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
Jun 8 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a

Christian Christmann wrote:
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.

Jun 8 '06 #2

P: n/a

Christian Christmann wrote:
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.

Jun 8 '06 #3

P: n/a

"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:pa****************************@yahoo.de...
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
Jun 8 '06 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christian Christmann wrote:
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

iD8DBQFEiDl4agVFX4UWr64RAr8rAJ4xqIMB529bJ7oa8a6Ejw PPZnQOLACffvVQ
z2FCJiZ0dum1DbgI+IgjHtI=
=Mwp0
-----END PGP SIGNATURE-----
Jun 8 '06 #5

P: n/a
On Thu, 08 Jun 2006 16:50:00 -0600,
Christian Christmann <pl*****@yahoo.de> wrote
in Msg. <pa****************************@yahoo.de>
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
Jun 8 '06 #6

P: n/a
"Christian Christmann" writes:
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.
Jun 8 '06 #7

P: n/a


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?

--
Er*********@sun.com

Jun 8 '06 #8

P: n/a
Christian Christmann wrote:

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:Th*************@gmail.com>

Jun 8 '06 #9

P: n/a
>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
Jun 8 '06 #10

P: n/a
Christian Christmann wrote:
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.

--
Chris "functioning & pointing" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Jun 8 '06 #11

P: n/a
Christian Christmann wrote:
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/
Jun 8 '06 #12

P: n/a
Christian Christmann wrote:
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.
Jun 9 '06 #13

P: n/a
"Sjouke Burry" <bu*************@ppllaanneett.nnlll> wrote in message
news:44**********************@text.nova.planet.nl. ..
Christian Christmann wrote:
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.


How would you implement quicksort for arbitrary data types without using
callback functions?
Jun 9 '06 #14

P: n/a
"Dann Corbit" <dc*****@connx.com> writes:
[...]
How would you implement quicksort for arbitrary data types without using
callback functions?


Incorrectly.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 9 '06 #15

P: n/a

Christian Christmann wrote:
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!!

Jun 9 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.