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

Asked

Viewed 40 times

0

I’m having some problems with the code below.

  1. The program compiles but an error occurs in the execution.
  2. From what I understand the problem is in printing... due to variable size size_t size of the struct.

I have no idea how to fix it. I am very rusty in programming, I appreciate the help of anyone who knows something. Follow 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;
}
  • It should run and close quickly. Use the printf inside main, if in output something appears it is because it is running. It is also probable that either vec->size be 0.

  • 1

    I can’t test this and even debug the problem. Isolating the problem to be sure where it occurs, I get the impression that is not where you imagine. That is, if you make a [mcve] you are likely to find the error alone. And this is a basic programming technique. Everyone should learn to isolate responsibilities, even when everything works out. When compiled with warnings linked, this code has several problems. It also has several vices.

  • I did these checks... The problem is this 'vec->size' right. I just don’t know how to fix it. Any suggestions?!

  • I’m not sure, but it seems the problem is here memcpy(&vec->vetor[i], &auxiliar, sizeof(auxiliar));

No answers

Browser other questions tagged

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