Can any one suggest the relative advantages of Function Pointers over directly calling the functions in C.
In which circumstances we realise the full efficiency of Function Pointers.
Thanx in Advance
Gobinath Krishna
Can any one suggest the relative advantages of Function Pointers over directly calling the functions in C.
In which circumstances we realise the full efficiency of Function Pointers.
Thanx in Advance
Gobinath Krishna
It's hard to tell, but first that cross my mind is something like this: (form Prelude's tutorial about Binary Serach Tree in the FAQ)
As you can see function poninter can parameter in another function.Code:Source 7: void pretree_levelorder ( struct pretree_node *header, void (*action) ( struct pretree_node *node ) ) { struct pretree_node *save[50]; int front = 0; int back = 0; if ( header == NULL ) return; save[front++] = header; while ( front != back ) { header = save[back++]; action ( header ); if ( header->link[0] != NULL ) save[front++] = header->link[0]; if ( header->link[1] != NULL ) save[front++] = header->link[1]; } }
It can be handy when you want to call same function with some date and but each time you need to perform some action on that data. For instance in one call action could be a simple printf to print data on the screen or in another call you can send another function through pointer that will perform some data processing.
I don't know if this helps, but you can find out a lot using google
I suggest you reading this:
http://www.cs.kau.se/~donald/ds_alg/...1_funcptr.html
hi man.
the advantage is the efficiency in access address in memory directly in functions....the method of indexation of vector is most waste that this, because the access with pointer work directly with aritmetmetic of pointer...in which is based in aritmetic binary of pointers that is more fast.
The pointer is useful whenever work with strings that in true strings are simply address consecutives location in memory.
Working with pointers in begin can be bit hard...but is just in begin...after seens work better than begin.
Last edited by Salem; 03-09-2005 at 12:34 PM. Reason: Edit profanity - read your PM and don't let me see this again!
?????????????????????????????????????????????????? ?????????????????????????????????
What?
Anyways, the advantage to using function pointers has nothing to do with efficiency. Whether it is a true function or a pointer to a function both methods will use an address in memory. The advantage is that you can link function pointers to functions - so you can unlink them just as easily. For instance let's say you have a function pointer called MathOperatoion.
Code:void (*MathOperation)(int operand1,int operand2,int *out);
Now you can create arithmetic functions.
The advantage here is we can link our function pointer to any of these functions.Code:void Add(int v1,int v2,int *out) { *out=(v1+v2); } void Subtract(int v1,int v2,int *out) { *out=(v1-v2); } void Multiply(int v1,int v2,int *out) { *out=(v1*v2); } void Divide(int v1,int v2,int *out) { *out(=v1/v2); }
This will call Add since we linked MathOperation to Add. The same can be done for all 4 functions.Code:... MathOperation=Add; int v1,v2,v3; MathOperation(v1,v2,&v3);
This is a stupid example and my pointers might be a bit off, but you get the idea.
A function pointer can simplify handling several versions of a routine. As another example, you might have some pixel blending functions for sprites (just the prototypes):
Then a global pointer is defined:Code:int blend_normal(int, int, int); int blend_add(int, int, int); int blend_subtract(int, int, int);
To select between functions, the pointer is simply redirected to the appropriate function:Code:int (*blend)(int, int, int);
So instead of having separate sprite-drawing routines for normal, add, and subtract, the sprite function would just use the global pointer for blending operations. The sprite function wouldn't know or care which blender is in use. Note that this doesn't improve performance, and it's a bit unstructured, but it might be space-efficient.Code:blend = blend_normal; blend = blend_add; etc.
The other catch is that i'm not sure what happens if you try and point to an function defined as inline. It appears to work, so I assume the compiler keeps normal, non-inlined copies?
It's also really useful for "plugins". I incorporated a plugin system to a MUD-ish server called TinyMAZE. Here's the code if you want to check it out:
http://www.tinymaze.com/plugins.c
http://www.tinymaze.com/plugins.h
If you understand what you're doing, you're not learning anything.
The only catch to function pointers is in C++ - you CANNOT have a function pointer in a class point to a function in another class.
Each function member in a class has an implicit _thiscall call type. This cannot be casted away and cannot be type-casted to another type like _stdcall or _pascal. I think the reasoning behind this is that you could override the inherent C++ protection mechanisms by using function pointers. So to avoid the abuse, I think they placed _thiscall there - also so that the compiler knows this function must be called from an object of that type.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.