--- In [email protected], "John Matthews" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], "Paul Herring" <pauljherring@> wrote:
> >
> > On Thu, Jul 3, 2008 at 11:16 AM, Arindam Biswas <biswaari@> 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.

...although, I guess if the variable is in a register, jumping to an
address value (no pointer) might even be less efficient than jumping
to an address in a register (pointer), because fetching the address
from program code might take longer than fetching it from a register?

Reply via email to