Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.compilers > #405 > unrolled thread
| Started by | Swati <swatirathi@cse.iitb.ac.in> |
|---|---|
| First post | 2012-01-06 12:13 +0530 |
| Last post | 2012-01-19 14:07 +0000 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.compilers
Help needed with function pointer examples Swati <swatirathi@cse.iitb.ac.in> - 2012-01-06 12:13 +0530
Re: Help needed with function pointer examples Uli Kusterer <ulimakesacompiler@googlemail.com> - 2012-01-15 03:41 +0100
Re: Help needed with function pointer examples arnold@skeeve.com (Aharon Robbins) - 2012-01-16 19:28 +0000
Re: Help needed with function pointer examples glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-01-15 11:27 +0000
Re: Help needed with function pointer examples torbenm@diku.dk (Torben Ægidius Mogensen) - 2012-01-16 17:33 +0100
Re: Help needed with function pointer examples Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2012-01-17 16:28 +0100
Re: Help needed with function pointer examples Tony Finch <dot@dotat.at> - 2012-01-19 14:07 +0000
| From | Swati <swatirathi@cse.iitb.ac.in> |
|---|---|
| Date | 2012-01-06 12:13 +0530 |
| Subject | Help needed with function pointer examples |
| Message-ID | <12-01-006@comp.compilers> |
Hello, We are doing an analysis on the usage of function pointers. We have considered one such example as GCC which uses function pointers. Can you suggest any other application which uses function pointers extensively. Regards, Swati
[toc] | [next] | [standalone]
| From | Uli Kusterer <ulimakesacompiler@googlemail.com> |
|---|---|
| Date | 2012-01-15 03:41 +0100 |
| Message-ID | <12-01-024@comp.compilers> |
| In reply to | #405 |
On 06.01.2012, at 07:43, Swati wrote:
> We are doing an analysis on the usage of function pointers.
> We have considered one such example as GCC which uses function pointers.
> Can you suggest any other application which uses function pointers
> extensively.
That is a vague, underspecified question if I ever heard one. What
exactly are you looking for with function pointers? Have you
implemented them in your compiler and want some examples to see if
your compiler implements everything necessary?
Anyway, the first example that comes to mind is any application that
can dynamically trigger actions, or that has to translate files or
data into actions. E.g. many interpreters and emulators essentially
look like this (untested C code fragment, never compiled):
struct Instruction { int instructionIndex; int param1; int param2 };
typedef void (*InstructionFuncPtr)( Instruction inInstruction );
InstructionFuncPtr gInstructions[2] = { AddInstruction, SubtractInstruction
};
void main(void)
{
Instruction vInstructions[10];
// ... read instructions from stdin or a file into 'vInstructions' ...
for( int x = 0; vInstructions[x] != -1; x++ )
gInstructions[ vInstructions[x].instructionIndex ]( vInstructions[x] );
return 0;
}
Where AddInstruction and SubtractInstruction are two functions that actually
implement the given operations.
Cheers,
-- Uli Kusterer
http://stacksmith.com
[toc] | [prev] | [next] | [standalone]
| From | arnold@skeeve.com (Aharon Robbins) |
|---|---|
| Date | 2012-01-16 19:28 +0000 |
| Message-ID | <12-01-030@comp.compilers> |
| In reply to | #423 |
I'm not intimately familiar with the code, but I'm pretty sure Brian Kernighan's The One True Awk works this way, as does the smaller "hoc" language presented in The Unix Programming Environment book. Arnold Uli Kusterer <ulimakesacompiler@googlemail.com> wrote: >On 06.01.2012, at 07:43, Swati wrote: >> We are doing an analysis on the usage of function pointers. >> We have considered one such example as GCC which uses function pointers. >> Can you suggest any other application which uses function pointers >> extensively. ...
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2012-01-15 11:27 +0000 |
| Message-ID | <12-01-026@comp.compilers> |
| In reply to | #405 |
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]
[toc] | [prev] | [next] | [standalone]
| From | torbenm@diku.dk (Torben Ægidius Mogensen) |
|---|---|
| Date | 2012-01-16 17:33 +0100 |
| Message-ID | <12-01-029@comp.compilers> |
| In reply to | #425 |
The moderator opined: > [Any program written in an OO language that allows inheritance or > overloading uses function pointers, since that's how they're > implemented. -John] If the inheritance and overloading can be resolved statically, function pointers are not needed. But most OO languages use dynamic method resolution by default, so what you say applies to nearly all OO implementations. torben
[toc] | [prev] | [next] | [standalone]
| From | Hans-Peter Diettrich <DrDiettrich1@aol.com> |
|---|---|
| Date | 2012-01-17 16:28 +0100 |
| Message-ID | <12-01-034@comp.compilers> |
| In reply to | #428 |
Torben Fgidius Mogensen schrieb: > The moderator opined: >> [Any program written in an OO language that allows inheritance or >> overloading uses function pointers, since that's how they're >> implemented. -John] > > If the inheritance and overloading can be resolved statically, function > pointers are not needed. But most OO languages use dynamic method > resolution by default, so what you say applies to nearly all OO > implementations. IMO the key is *virtual* methods, which require dynamic lookup in a Virtual Method Table or similar structure. In non-OO languages calling conventions like "call by name" also can require dynamic lookup of function addresses. Sorting and searching algorithms also typically are implemented with a compare function (pointer) argument. DoDi
[toc] | [prev] | [next] | [standalone]
| From | Tony Finch <dot@dotat.at> |
|---|---|
| Date | 2012-01-19 14:07 +0000 |
| Message-ID | <12-01-037@comp.compilers> |
| In reply to | #405 |
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. >Can you suggest any other application which uses function pointers >extensively. Function pointers are often a nice way to implement state machines, for instance http://dotat.at/cgi/git?p=unifdef.git;a=blob;f=unifdef.c The byte code in a threaded code interpreter is often just a list of function pointers. There are a lot of function pointers in the unix kernel. Have a look at Lions' commentary, Stevens' TCP/IP Illustrated volume 2, and McKusick & Neville-Neil's Design and Implementation of the FreeBSD operating system. Tony. -- f.anthony.n.finch <dot@dotat.at> http://dotat.at/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.compilers
csiph-web