Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointers to functions

Reply
Thread Tools

Pointers to functions

 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-08-2006
-----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-----
 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      06-08-2006

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.

 
Reply With Quote
 
 
 
 
Suman
Guest
Posts: n/a
 
      06-08-2006

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.

 
Reply With Quote
 
Allan M. Bruce
Guest
Posts: n/a
 
      06-08-2006

"Christian Christmann" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
Robert Latest
Guest
Posts: n/a
 
      06-08-2006
On Thu, 08 Jun 2006 16:50:00 -0600,
Christian Christmann <(E-Mail Removed)> wrote
in Msg. <(E-Mail Removed)>

> 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
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-08-2006
"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.


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      06-08-2006
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: <(E-Mail Removed)>

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-08-2006


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?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      06-08-2006
>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
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      06-08-2006
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do not cast pointers to functions to pointers to primitive types ajaybgr C Programming 18 09-07-2012 04:34 AM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Pointers to char pointers in functions newbie C Programming 9 09-24-2006 10:31 AM
Questions about pointers to objects and pointers to functions Marc Thrun C Programming 15 10-04-2005 05:47 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM



Advertisments