Pointers to functions

Discussion in 'C Programming' started by Lew Pitcher, Jun 8, 2006.

  1. Lew Pitcher

    Lew Pitcher Guest

    -----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

    iD8DBQFEiDl4agVFX4UWr64RAr8rAJ4xqIMB529bJ7oa8a6EjwPPZnQOLACffvVQ
    z2FCJiZ0dum1DbgI+IgjHtI=
    =Mwp0
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Jun 8, 2006
    #1
    1. Advertising

  2. Lew Pitcher

    Vladimir Oka Guest

    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.
     
    Vladimir Oka, Jun 8, 2006
    #2
    1. Advertising

  3. Lew Pitcher

    Suman Guest

    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.
     
    Suman, Jun 8, 2006
    #3
  4. "Christian Christmann" <> wrote in message
    news:p...
    > 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
     
    Allan M. Bruce, Jun 8, 2006
    #4
  5. On Thu, 08 Jun 2006 16:50:00 -0600,
    Christian Christmann <> wrote
    in Msg. <>

    > 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
     
    Robert Latest, Jun 8, 2006
    #5
  6. Lew Pitcher

    osmium Guest

    "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.
     
    osmium, Jun 8, 2006
    #6
  7. 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:>
     
    Kenneth Brody, Jun 8, 2006
    #7
  8. Lew Pitcher

    Eric Sosman Guest

    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?

    --
     
    Eric Sosman, Jun 8, 2006
    #8
  9. >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
     
    Gordon Burditt, Jun 8, 2006
    #9
  10. Lew Pitcher

    Chris Dollin Guest

    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/
     
    Chris Dollin, Jun 8, 2006
    #10
  11. 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/
     
    Giannis Papadopoulos, Jun 8, 2006
    #11
  12. 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
     
    Christian Christmann, Jun 8, 2006
    #12
  13. Lew Pitcher

    Sjouke Burry Guest

    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.
     
    Sjouke Burry, Jun 9, 2006
    #13
  14. Lew Pitcher

    Dann Corbit Guest

    "Sjouke Burry" <> wrote in message
    news:4488bebd$0$2022$...
    > 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?
     
    Dann Corbit, Jun 9, 2006
    #14
  15. "Dann Corbit" <> writes:
    [...]
    > How would you implement quicksort for arbitrary data types without using
    > callback functions?


    Incorrectly.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jun 9, 2006
    #15
  16. Lew Pitcher

    Naresh Guest

    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!!
     
    Naresh, Jun 9, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S?ren Gammelmark
    Replies:
    1
    Views:
    1,893
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    861
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    308
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    680
  5. ajaybgr
    Replies:
    18
    Views:
    975
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page