0
I am facing some difficulties with the code snippet below:
The call of this function is:
cache = OperaCache(true , endereco, op, cache, descricao, read_hits, read_misses, write_hits, write_misses);
VETOR_CACHE OperaCache(bool contar, unsigned long long int endereco, char op, VETOR_CACHE cache, CACHEDESC descricao, int *read_hits, int *read_misses, int *write_hits, int *write_misses)
clock_t tempo_menor = clock();//tempo maior que os outros
int trocaIndex=-1, indexRead=-1, indexWrite=-1;//inicia sem troca
bool readAchou=false, writeAchou=false;
unsigned int i;
for(i=0; i<descricao.number_of_lines; i++)
{
if(index==cache.vetor[i].index)
{
switch (op)
{
case 'R':
if(!readAchou && (tag==cache.vetor[i].tag))
{
readAchou=true;
indexRead=i;
}
break;
case 'W':
if(!writeAchou)
{
if(tag==cache.vetor[i].tag)
{
writeAchou=true;
indexWrite=i;
}
else if(cache.vetor[i].time<=tempo_menor)
{
tempo_menor=cache.vetor[i].time;
trocaIndex=i;
}
}
break;
default:
printf("Operacao invalida encontrada.");
}
}
}
switch (op)
{
case 'R':
if(readAchou)
{
if((strcmp(descricao.replacement_policy,"LRU")) == 0)
cache.vetor[indexRead].time=clock();
if(contar)
*read_hits+=1;
}
else
{
if(contar)
*read_misses+=1;
cache = OperaCache(false, endereco, 'W', cache, descricao, read_hits, read_misses, write_hits, write_misses);
}
break;
case 'W':
if(writeAchou)
{
cache.vetor[indexWrite].time=clock();
if(contar)
*write_hits+=1;
}
else if(trocaIndex>-1)
{
if(contar)
*write_misses+=1;
cache.vetor[trocaIndex].tag=tag;
cache.vetor[trocaIndex].time=clock();
}
break;
default: printf("Operacao invalida encontrada.");
}
The way out should be:
- Read hits:64445
- Read Misses:158
- Write hits:44056
- Write Misses:315
But for some reason it’s being:
- Read hits:64603
- Read Misses:0
- Write hits:44371
- Write Misses:0
Before:
In short, my problem is (I imagine) in the flags
readAchou
,writeAchou
and/orcontar
that for some reason are skipping the Misses cases and considering all hits...
Fix, the error really is in the flag contar
, but I have no idea how to fix it.
Other parts of the code are irrelevant to doubt...
– Oberdan Santos
google : "How to debug a code"
– Guilherme Lautert
Can you make an example smaller and easier to understand? http://answall.com/help/mcve
– hugomg
@hugomg Beauty, I make a logo and put here.
– Oberdan Santos
You who happens to be able to count Boolean, and you only consult, so it doesn’t make much sense to be giving dick. Try to put a print at the bottom of each check of the count, identifying which part of the code it entered and which value of the variable, so you get a better understanding of what’s going on.
– leofontes