The compilation is done without errors or warnings ... But the program does not execute!

0

I have some problems with the code below.

  • The program compiles but an error occurs in the execution.
  • I noticed the problem is in print ... due to the variable size size_t size of the struct.
  • I have no idea how to fix this. I'm very rusty in programming, thank you for the help of someone who knows something. Here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <time.h>
    
    typedef struct cachedesc{
        int line_size;
        int number_of_lines;
        int associativity;
        char replacement_policy[4];
    }CACHEDESC;
    
    typedef struct cache{
        int tag;
        int index;
        int data;
        clock_t time;
    }CACHE;
    
    typedef struct vetor_cache{
        size_t size;
        CACHE *vetor;
    } VETOR_CACHE;
    
    
    CACHEDESC LeCachedesc()
    {
        char line_size_aux[10], number_of_lines_aux[10], associativity_aux[10], replacement_policy_aux[4]={};
        CACHEDESC auxiliar;
        int i=0;
        char ch;
        FILE *arq;
    
        arq = fopen("cachedesc.dat", "r");
        if(arq == NULL)
            printf("Erro, nao foi possivel abrir o arquivo\n");
        else
        {
            fseek(arq,12,SEEK_SET);
            while( (ch=fgetc(arq))!= '\n' )
            {
                line_size_aux[i] = ch;
                i++;
            }
            i=0;
            fseek(arq,34,SEEK_SET);
            while( (ch=fgetc(arq))!= '\n' )
            {
                number_of_lines_aux[i] = ch;
                i++;
            }
            i=0;
            fseek(arq,55,SEEK_SET);
            while( (ch=fgetc(arq))!= '\n' )
            {
                associativity_aux[i] = ch;
                i++;
            }
            i=0;
            fseek(arq,79,SEEK_SET);
            while( (ch=fgetc(arq))!= EOF )
            {
                replacement_policy_aux[i] = ch;
                i++;
            }
        }
        fclose(arq);
        auxiliar.line_size = atoi(line_size_aux);
        auxiliar.number_of_lines = atoi(number_of_lines_aux);
        auxiliar.associativity = atoi(associativity_aux);
        strcpy(auxiliar.replacement_policy, replacement_policy_aux);
    
        return auxiliar;
    }
    
    void printaCache(VETOR_CACHE* vec) {
        int i;
        for (i = 0; i < vec->size; i++) {
            printf("%d \t %d \t %d \t %d\n", i, vec->vetor[i].tag, vec->vetor[i].index, vec->vetor[i].time);
        }
    }
    
    VETOR_CACHE* createCache(CACHEDESC desc) 
    {
        CACHE auxiliar;
        VETOR_CACHE* vec = (VETOR_CACHE*)malloc(desc.number_of_lines * (sizeof auxiliar));
        int associatividade = desc.associativity;
        int i, contador=0;
        for (i = 0; i < desc.number_of_lines; i++) 
        {
            auxiliar.tag = 0;
            auxiliar.index = contador;
            auxiliar.data = 0;
            auxiliar.time = clock();//start times
            memcpy(&vec->vetor[i], &auxiliar, sizeof(auxiliar));
    
            --associatividade;
    
            if (associatividade == 0) {
                associatividade = desc.associativity;
                contador++;
            }
        }
        return vec;
    }
    
    
    int main()
    {
        int line_size=0, number_of_lines=0, associativity=0;
        int access_count=0, read_hits=0, read_misses=0, write_hits=0, write_misses=0;
    
        {
            CACHEDESC descricao = LeCachedesc ();
            printf("%d\n%d\n%d\n%s\n", descricao.line_size, descricao.number_of_lines, descricao.associativity, descricao.replacement_policy);
    
            VETOR_CACHE* cache = createCache(descricao);
    
            printaCache(cache);
    
        } // ESCOPO PRINCIPAL
    
        return 0;
    }
    
        
    asked by anonymous 19.11.2016 / 17:59

    0 answers