Function Pointers, part 1
(Page 1 of 4 )
In this first of a three-part series of articles, J. Nakamura covers regular function pointers. These pointers allow you to access the code you write for your applications in the form of functions.
Introduction
In a previous series of articles named “Pointer Perfect,” I looked at how data stored in computer memory is accessible through pointers. The code you write for your application in the form of functions (these can be global or class member functions) is very much accessible through pointers in the same way, and I will introduce you to the syntax and usage of these function pointers in this article.
Function pointers are great for implementing callbacks and are very useful when you want to introduce late binding in your application. They help you parse configuration files, help you read protocols and can help you store a function call in an object when you want to delay or record the call (very useful in an undo/redo system).
Most C++ books treat function pointers superficially, which is very unfortunate; maybe this is because their syntax looks a bit weird when you first start using them. I hope to be able to convince you that the right function pointer at the right moment in the right place will make your code clearer, cleaner and easier to understand. Once you get past the odd syntax and come to understand the power of calling functions through pointers, a whole new level of abstraction will open up to you.
A Quick Example There is nothing like a quick example to take you straight to the heart of a topic. Here is a simple code example that uses a function pointer.
// main.cpp
#include
int IncOne(int var) {
return ++var;
}
int main(int argc, char *argv[]) {
// creating pointer to function
int (*inc_one)(int) = &IncOne;
// use the function the standard way
int result = IncOne(1);
(void)printf(“result is: %d\n”, result);
// utilize the function pointer
result = inc_one(1);
(void)printf(“result is: %d\n”, result);
return 0;
}
When you run this simple example, you will notice that both times the result printed is "2" -- as expected, of course. So what has the compiler come up with for us? Let’s take a peek into the debugger at the value stored in inc_one.
int (*inc_one)(int) = &IncOne;
00411A9E mov dword ptr [inc_one],offset IncOne (411523h)
It holds the address 0x00411523, and looking at the memory at that position we find the following line:
0041126C jmp IncOne (411A40h)
Our code is ordered to jump to 0x411A40, where we of course will find the code of our IncOne() function:
int IncOne(int var)
{
00411A40 push ebp
00411A41 mov ebp,esp
...
}
So what does the assembly in the debugger look like when we make a normal function call to IncOne() ?
int result=IncOne(1);
00411AA5 push 1
00411AA7 call IncOne (41126Ch)
Hold on! The debugger tells us that we go to 0x0041126c, where we are made to jump to IncOne (0x00422A40). So what happens when we call the function using the function pointer inc_one?
result=inc_one(1);
00411AC3 mov esi,esp
00411AC5 push 1
00411AC7 call dword ptr [inc_one]
Exactly the same thing! The content of inc_one is 0x0041126C, which, when called, makes our code jump to IncOne (0x00422A40), where the function is executed. This basically covers the whole concept of function pointers. All that is left now are the many (strange) forms in which they can appear.
Next: Regular Function Pointer Syntax >>
More C++ Articles
More By J. Nakamura