2-Circular Buffer - Function - Pointers
2-Circular Buffer - Function - Pointers
2-Circular Buffer - 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
fim=4
Adicionando 1 elemento
ini=1
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;
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;
}
Exercise
}
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
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
//Function to be called
void nop (void){ __asm NOP __endasm }
U
UNN II F
FEE II
Exercise
U
UNN II F
FEE II