Counters not being incremented in code

0

I'm having some difficulties with the code snippet below:

The call to 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 output 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 / or contar that for some reason are jumping misses cases and considering everything hits ...

    Correcting, the error is actually in the contar flag, however I have no idea how to fix it.

        
    asked by anonymous 23.11.2016 / 13:50

    1 answer

    0

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

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

        
    24.11.2016 / 22:16