What is function pointer

A function pointer contains the address of the location where the code (in the code segment) of that function resides in a process. But what is a code segment? What is a process?

Process and Code Segment

Process is a program loaded in memory. As simple as this. An executable file is a program and when its run it becomes a process. Lets take an example of the following program.

int nextNumber(int n)
{
return n + 1;
}

int main()
{
const int t = 5;
int x = nextNumber(3);
printf("%d\n", x);
return 0;
}

After the above code is compiled it should generate an executable program which when run calls the function nextNumber and prints that number.

That means the function nextNumber exists somewhere in the program. Right? Infact the function main and the function printf exists in the program! The entire code gets compiled to native code and an executable is generated. Native code means the instruction set which the OS understands on which the program is compiled.

So this program has all the source code and compiled to native code and is present in the “code segment” of the program. In other words the section of the compiled program where the source code is present is called code segment. It is also called text segment.

What is Data Segment

Its that section of the program where the global and static variables are stored. The data segment is sub divided into initialised and uninitialized data segment.

static int  i = 5; // <-- This code will **NOT** be in code segment but goes into data segment; in initialised data segment

static int  j; // <-- This code will **NOT** be in code segment but goes into data segment; in uninitialised data segment

Now, when this program is run it becomes a process. The code segment and the data segment is loaded into the memory. When the program is executed it may allocate and deallocate memory.

A program does not have any memory. Its just a file with code and data segment which is capable of getting executed.

What is a Symbol Table

A symbol table is a table that contains mapping of a C++ symbol with the corresponding actual value when the program is run.

What goes into symbol table?

The functions and constant variables! It should look something like this…

Capture

So, what is a function pointer and what does the function pointer contain?

Function pointer is a pointer that points to a function! Like char* points to a char data type and float* points to float data type. A function pointer is a pointer that points to what type? “function”.

If there is a function

return_type creepy_function(arg1_type arg1, arg2_type arg2)

the “function pointer type” will look like

return_type (*new_function_pointer)(arg1_type arg1, arg2_type arg2) = NULL

new_function_pointer is a pointer to a function that returns a “return_type” and takes two arguments as shown.

The statement

new_function_pointer = creepy_function

will result in copying the address of the creepy_function in the new_function_pointer. The creepy_function will have a location in code segment when the program is run.

A far better explanation probably exists here => http://www.cprogramming.com/tutorial/function-pointers.html

One thought on “What is function pointer

Comments are closed.