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 15 2406
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.
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.
"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
-----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-----
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
"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.
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
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>
>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
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/
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/
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" <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?
"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.
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!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex |
last post by:
Is there any problem with sending function pointers through in a
variable argument list? I have a function like the following:
typedef (*ptr2FuncType1)( int );
typedef (*ptr2FuncType2)( double...
|
by: Thomas Matthews |
last post by:
Hi,
I would like to create a table (or vector) of pointers
to templated functions.
1. How do I declare a typedef of a pointer to a templated
function?
For example, I have some functions...
|
by: Mikhail Teterin |
last post by:
Hello!
I'd like to have a variable of a pointer-to-function type. The two possible
values are of type (*)(FILE *) and (*)(void *). For example:
getter = straight ? fgetc : gzgetc;
nextchar...
|
by: munni |
last post by:
hi
i want to write a program on function pointers where i have not
written any programs using function pointers till now. i want to take
an array of 26 functions which r to be pointed by this...
|
by: cps |
last post by:
Hi,
I'm a C programmer taking my first steps into the world of C++.
I'm currently developing a C++ 3D graphics application using GLUT
(OpenGL Utility Toolkit written in C) for the GUI...
|
by: Bushido Hacks |
last post by:
A private utility function using a function pointer sounds ideal to
me. I want to simpify writing the same set of loops. While there are
a few functions that can't use it, setting and modifying...
|
by: claudiu |
last post by:
Hi,
I'll go straight to the first question. Why does the code below
compile?
void f(int i = 0);
int main()
{
(&f)();
|
by: bgold |
last post by:
Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair...
|
by: a |
last post by:
Hello.
Suppose I have a family of functions
f_0(x) = 0*x
f_1(x) = 1*x
....
f_n(x) = n*x
taking float and returning float (naturally, the actual functions would
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |