Counters are not being incremented in code

Asked

Viewed 59 times

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:

  1. Read hits:64445
  2. Read Misses:158
  3. Write hits:44056
  4. Write Misses:315

But for some reason it’s being:

  1. Read hits:64603
  2. Read Misses:0
  3. Write hits:44371
  4. Write Misses:0

Before:

In short, my problem is (I imagine) in the flags readAchou, writeAchou and/or contar 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.

  • 1

    Other parts of the code are irrelevant to doubt...

  • 1

    google : "How to debug a code"

  • Can you make an example smaller and easier to understand? http://answall.com/help/mcve

  • @hugomg Beauty, I make a logo and put here.

  • 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.

1 answer

0

In the function signature you define: int *read_hits, int *read_misses, int *write_hits, int *write_misses or pointers to integer.

In function call it is not clear that you are passing pointers to variables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.