Re: Help needed with function pointer examples

glen herrmannsfeldt <gah@ugcs.caltech.edu>
Sun, 15 Jan 2012 11:27:07 +0000 (UTC)

          From comp.compilers

Related articles
Help needed with function pointer examples swatirathi@cse.iitb.ac.in (Swati) (2012-01-06)
Re: Help needed with function pointer examples ulimakesacompiler@googlemail.com (Uli Kusterer) (2012-01-15)
Re: Help needed with function pointer examples gah@ugcs.caltech.edu (glen herrmannsfeldt) (2012-01-15)
Re: Help needed with function pointer examples torbenm@diku.dk (2012-01-16)
Re: Help needed with function pointer examples arnold@skeeve.com (2012-01-16)
Re: Help needed with function pointer examples DrDiettrich1@aol.com (Hans-Peter Diettrich) (2012-01-17)
Re: Help needed with function pointer examples dot@dotat.at (Tony Finch) (2012-01-19)
| List of all articles for this month |

From: glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups: comp.compilers
Date: Sun, 15 Jan 2012 11:27:07 +0000 (UTC)
Organization: Aioe.org NNTP Server
References: 12-01-006
Keywords: analysis, comment
Posted-Date: 16 Jan 2012 00:29:39 EST

Swati <swatirathi@cse.iitb.ac.in> wrote:


> We are doing an analysis on the usage of function pointers.
> We have considered one such example as GCC which uses function pointers.


You don't specify the language, maybe it matters, maybe not.


Long before function pointers as we usually think of them, Fortran had
the ability to pass a function name as an actual argument. That lets
the called routine call the specified function, but there is no
provision to store such a pointer for later use.


This allows one to write minimization or integration routines (two
popular uses) independent of the function being minimized or
integrated. In a similar way, C's qsort() allows one to supply a
compare routine which is called, but a function pointer isn't saved.


On the other hand, the unix I/O system is built around a structure
array, one element per major device type, containing function
pointers. The pointers might even be compile-time constants. At run
time, then, the I/O system uses a device code to index into the array,
select the appropriate routine for the device, and call it. For many
years, a C compiler was required to sysgen unix, even if the only need
was to compile that table.


> Can you suggest any other application which uses function pointers
> extensively.


I don't know about extensively. Both types of applications are much
more convenient when done using function pointers.


For the former case, you can instead substitute the required function
into the source, compile it and debug it. (For open source routines.)


For the second, you can instead use an indexed jump, such as
switch/case, to a series of function calls.


PL/I has ENTRY variables, which perform a similar function, but
aren't called function pointers.


-- glen
[Any program written in an OO language that allows inheritance or
overloading uses function pointers, since that's how they're
implemented. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.