0% found this document useful (0 votes)
32 views

2-Circular Buffer - Function - Pointers

The document discusses circular buffers and function pointers. Circular buffers are data structures that use a "wrap-around" approach to store temporary data in a fixed-size block of memory. They can be implemented using vectors or linked lists. Function pointers allow functions to be called indirectly by storing the address of a function in a pointer variable. This allows functions to be executed dynamically at runtime rather than being fixed at compile time. The document provides examples of implementing a circular buffer and using function pointers to call functions indirectly through a pointer in C code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

2-Circular Buffer - Function - Pointers

The document discusses circular buffers and function pointers. Circular buffers are data structures that use a "wrap-around" approach to store temporary data in a fixed-size block of memory. They can be implemented using vectors or linked lists. Function pointers allow functions to be called indirectly by storing the address of a function in a pointer variable. This allows functions to be executed dynamically at runtime rather than being fixed at compile time. The document provides examples of implementing a circular buffer and using function pointers to call functions indirectly through a pointer in C code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Function pointers

ECOS03 - EOS
Review

Pointers/Structs
Circular buffers
Circular Buffers
• “Endless” memory spaces
• Use FIFO aproach
• Store temporary data
• Can implemented using vectors or linked-lists

U
UNN II F
FEE II
Circular Buffers
• Vector implementation
 Uses less space
 Need special caution when cycling
 Problem to differentiate full from empty

U
UNN II F
FEE II
Circular Buffers

[0]
[4] [1]

[3] [2]
U
UNN II F
FEE II
Buffer Vazio

ini=0

? ? ? ? ?
[0] [1] [2] [3] [4]

fim=0
Adicionando 2 elementos

ini=0

'A' 'B' ? ? ?
[0] [1] [2] [3] [4]

fim=2
Removendo 1 elemento

ini=1

'A' 'B' ? ? ?
[0] [1] [2] [3] [4]

fim=2
Adicionando 2 elementos

ini=1

'A' 'B' 'C' 'D' ?


[0] [1] [2] [3] [4]

fim=4
Adicionando 1 elemento

ini=1

'A' 'B' 'C' 'D' 'E'


[0] [1] [2] [3] [4]

fim=0
Circular buffers

#define CB_SIZE 10

int circular_buffer[CB_SIZE];

int index=0;

for(;;){
//do anything with the buffer
circular_buffer[index] = index;
//increment the index
index = (index+1)%CB_SIZE;
}
Circular buffers
#define CB_SIZE 10
int circular_buffer[CB_SIZE];
int start=0, end=0;

char AddBuff(int newData)


{
//check if there is space to add a number
if ( ((end+1)%CB_SIZE) != start)
{
circular_buffer[end] = newData;
end = (end+1)%CB_SIZE;
return SUCCESS;
}
return FAIL;
}
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();
}
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?

http://hackalizer.com/wrap-your-head-around-function-pointers-in-c/

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
Exercise

Using 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

You might also like