Pointers to functions!

Hi guys,
Here is an example about pointer to function (was mentioned in the tutorial):
// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}

int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
-----------------------------------

When I debugged it, I tracked after the values:
functocall
addition
subtraction

Which supposed to be the pointers for these functions.
The first time we call the function "operation":
m = operation (7, 5, addition);
The value of "functocall" should be the same as "addition", but it's not what I got.
What I got was:
functocall = 0x0041127b
addition = 0x004117a0

Is there anyone has any idea why this happens?!
though i was sure it should be same as you are passing the function addresses.. but still i debugged it in gdb and the results are below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Breakpoint 1, main () at test.cpp:20
20		int (*minus)(int,int) = subtraction;
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686 libgcc-4.3.2-7.i386 libstdc++-4.3.2-7.i386
(gdb) next
22		m = operation (7, 5, addition);
(gdb) step
operation (x=7, y=5, functocall=0x80485a4 <addition(int, int)>) at test.cpp:13
13		g = (*functocall)(x,y);
(gdb) next
14		return (g);
(gdb) print functocall
$1 = (int (*)(int, int)) 0x80485a4 <addition(int, int)>
(gdb) print addition
$2 = {int (int, int)} 0x80485a4 <addition(int, int)>
(gdb)


you need to recheck what you are doing..
Last edited on
The way the function pointer system works isn't direct,
it is indirect through a Function Address Table.
Thank you for the reply :)

The strange thing is when I print the output on the screen I see the same addresses as expected:
functocall 00411050
addition 00411050

But in the watch window (I use visual studio 2008), the coming results are presented:
functocall 00411050
addition 004117b0

The same address for functocall, but not for addition!
Any idea why this happens?
Yes (ish).
functiontocall is a pointer to a function. It points to the address in the
Function Address Table (let's call it FAT) that holds the actual address of the addition function.

In your case, the address that it points to in the FAT is 00411050. (if you open a memory window and check that address you will see that it holds 004117b0 ).

The watch window and cout behaves differently

The Watch window simply dereferences the functocall pointer and gets 00411050. For Addition the watch windows will show the actual address which in this case is 004117b0

In the case of cout, the functocall pointer is fully dereferenced to get the actual address of the function.
So cout << functocall << " " << addition shows the same value

**Please note: feel free to jump in and correct me - is the FAT really a jump table??**
Last edited on
Topic archived. No new replies allowed.