--- In [email protected], "Paul Herring" <[EMAIL PROTECTED]> wrote: > > On Thu, Jul 3, 2008 at 11:16 AM, Arindam Biswas <[EMAIL PROTECTED]> wrote: > > Hi Friends, > > I need to understand a very simple doubt: > > Function pointer has some overhead associated with it. Still WHY SHOULD we use > > function pointer instead of calling directly that function. > > There is no difference between a 'function' and a 'function pointer' > except the syntax. The function has parentheses after it: > > int foo(void){ > return 1; > } > > int (bar*)(void) = foo; > > foo(); // calls foo > bar(); // calls foo - no additional overhead from the line above.
In general (ie. ignoring any optimisation which the compiler might perform on simple examples such as above), I would expect the compiler/linker to know the address of a function at build time, and substitute that address into the generated code. With a function pointer, the compiler/linker only knows the address of the function pointer variable, and has to generate code to read the variable before jumping to the address contained in that variable. John
