Function pointers
ECOS03 - EOS
Review
Circular buffer
Exercise
• Implement a circular buffer
Use a 10-position vector
• Each element of the vector is a structure with two
variables
char * ProcessName
int Time
• Create one function to add new elements and one to
remove the oldest elements.
U
UNN II F
FEE II
Exercise
• Implement a circular buffer
Use a 10-position vector
• Each element of the vector is a structure with two
variables
char * ProcessName
int Time
• Create one function to add new elements and one to
remove the oldest elements.
U
UNN II F
FEE II
Exercise
//definição da estrutura
typedef struct {
char* nome;
int time;
}process;
//definição do buffer circular
#define BUFFERSIZE 10
process buffer[BUFFERSIZE];
//definição dos “ponteiros” de acesso
int start, end;
Exercise
//função de adição de “process” no buffer
void addProc(char *nnome, int ntime){
//checagem de espaço disponível
if ( ((end+1)%BUFFERSIZE) != start){
//Atualização da posição atual
buffer[end].nome = nnome;
buffer[end].time = ntime;
//incremento da posição
end = (end+1)%(BUFFERSIZE);
}
}
Exercise
//função de remoção de um “process” do buffer
void removeProc (void){
//checagem se existe alguem pra retirar
if (end != start){
//incremento da posição
start = (start +1)%(BUFFERSIZE);
}
}
Exercise
#include "stdio.h"
void main (void){
addProc("proc1", 0);
addProc("proc2", 1);
addProc("proc3", 2);
removeProc();
removeProc();
removeProc();
}
Void pointers
Void pointers
• Points to a memory region without specifying the
type.
• Can not be used without casting.
• Abstraction that allows the programmer to pass the
parameter of different types to the same function.
• A function that receives how to know how to handle
each type.
U
UNN II F
FEE II
Void pointers
char *name = "Paulo";
double weight = 87.5;
unsigned int children = 3;
void main (void){
//não confundir com printf
print(0, name);
print(1, &weight);
print(2, &children);
}
Void pointers
void print(int option; void *parameter){
switch(option){
case 0:
printf("%s",(char*)parameter);
break;
case 1:
printf("%f",*((double*)parameter));
break;
case 2:
printf("%d",*((unsigned int*)parameter));
break;
}
}
Software engines
Software engines
• Necessity:
Make an image editor
that can choose the right
function to call
• 1st Implementation
Use a option parameter
as a switch operator
U
UNN II F
FEE II
Software engines
image Blur(image nImg){}
image Sharpen(image nImg){}
image imageEditorEngine(image nImg, int opt){
image temp;
switch(opt){
case 1:
temp = Sharpen(nImg);
break;
case 2:
temp = Blur(nImg);
break;
}
return temp;
}
Function pointers
Problem
• How to execute a function that is not known at
compile time?
Know the address of the function at runtime.
Stack the parameters correctly that the function needs
Make a function call to this address
U
UNN II F
FEE II
Function pointers
• Work almost as a normal pointer
• Its manipulation obeys all pointer manipulation
rules.
• Hold the address of a function start point instead the
address of a variable
• The compiler need no known the function signature
to pass the correct parameters and the return value.
• Awkard declaration (it is best to use a typedef)
U
UNN II F
FEE II
Where/why is this important/useful?
[Link]
U
UNN II F
FEE II
Function pointers
//defining the type pointerTest
//it is a pointer to function that:
// receives no parameter
// returns no parameter
typedef void (*pointerTest)(void);
//Function to be called
void nop (void){ __asm NOP __endasm }
//creating an pointerTest variable;
pointerTest foo;
foo = nop;
(*foo)(); //calling the function via pointer
Function pointers
Re-code the image editor engine using function pointers
Function pointers
image Blur(image nImg){}
image Sharpen(image nImg){}
typedef image (*ptrFunc)(image nImg);
//image editor engine
image imageEditorEngine(ptrFunc function,
image nImg){
image temp;
temp = (*function)(nImg);
return temp;
}
Function pointers
• Good • Bad
New function additions More complex code
do not alter the engine (function pointers are
The engine only needs not easy to understand
to be tested once for beginners)
Can change the function Probable bugs
implementations Lack of compile time
dynamically guarantees (function
signature)
U
UNN II F
FEE II
Lab
Update last class example to function pointers
• Update last class structure to void main (void){
include a function pointer as AddProc("name1", 1, func1);
one of its members. AddProc("name2", 2, func2);
• Create a function (ExecProc) AddProc("name3", 3, func3);
that executes the pointer AddProc()
stored in the "first" filled ExeProc();
position of the circular buffer. RemoveProc();
• Create a main that executes ExeProc();
the commands to the side: RemoveProc();
• Create the three different ExeProc();
functions, each printing a RemoveProc();
different phrase. }
U
UNN II F
FEE II