Printing a vector ... Differences between C ++ and C ... Where did I go wrong?

2

I have problems in vector printing of structs in C, in C ++ it worked ...

First I will show the C version with problems (in the execution since it compiles without errors)

CACHE cache = createCache(descricao); //chamada da main

printaCache(cache); //chamada da main

Now the functions:

CACHE createCache(CACHEDESC desc)
{
    int i, associatividade, contador=0;
    CACHE *vec;
    vec = malloc(desc.number_of_lines * (sizeof vec));

    associatividade = desc.associativity, contador = 0;
    for (i = 0; i < desc.number_of_lines; i++) {

        CACHE auxiliar;
        auxiliar.tag = 0;
        auxiliar.index = contador;
        auxiliar.data = 0;
        auxiliar.time = clock();//start times
        vec[i] = auxiliar;
        --associatividade;
        if (associatividade == 0)
        {
            associatividade = desc.associativity;
            contador++;
        }
    }
    return *vec;
}

void printaCache(CACHE vec)
{
    int i;
    for(i=0;i<sizeof(vec);i++)
    {
         printf("%d \t %d \t %d \t %d\n",i, vec);
    }
}

These functions compile without errors or warnings, but when I run the program it crashes ...

The code in C ++ that works 100% is:

void printCache(vector<CACHE> cache){
    for(int i=0; i<cache.size(); i++){
        cout<< i<< "\t "<< cache[i].tag << " \t " << cache[i].index <<"\t "<< cache[i].time  << "\n";
    }
 } 

vector<CACHE> createCache(CACHECONFIG conf){
    vector<CACHE> vec;
    int assoc = conf.associativity, count=0;
    for(int i=0; i<conf.numLines; i++){
        CACHE aux;
        aux.valido=true;
        aux.tag=0;
        aux.index=count;
        aux.dado=0;
        aux.time = clock();//inicio dos tempos
        vec.push_back(aux);
        if(--assoc==0){
            assoc=conf.associativity;
            count++;
             } 
        }
        printCache(vec);
        return vec;
    }

Both languages are using structs:

typedef struct cache{
    int tag;
    int index;
    int dado;
    clock_t time;
}CACHE;

Anyway, I'd like to know what I'm missing in C code for not getting the same C ++ code result

    
asked by anonymous 18.11.2016 / 05:08

1 answer

2

At answer to your previous question I said that it was not so simple to do this conversion. There will be a lot of different situation that will need proper treatment. The solution that seems simple is not very suitable. This code will be a maintenance nightmare. But let's try to solve one problem at a time, even though it's not the right thing to do.

The biggest problem there is that size() of vector does not convert to sizeof . The first really gives the size in number of elements of the vector. The second gives the size in number of bytes of a known data structure at compile time. Because this structure is dynamically created at runtime, you simply can not use sizeof . You really can not figure out the size.

The naive solution would be to carry the size back and forth to be used. But this will make the whole code difficult. The most correct would be to do what I said in that answer and create your own vector where you have the same semantics, so you can do the conversion quietly without a headache. That is, the naive answer there in the previous question did not work, other than that it even had errors and that does not work as you imagine. I knew this would happen.

The right thing is to make a complete structure, but I will give the solution only to this problem, which seems to be what you want. But note that I will make the solution as naive as possible, other problems will occur later. Since I do not have enough information to test, I can not guarantee that everything is right. I can only respond on what the question gives me.

Normally when it is exercise I do not even care that op code is not as suitable as possible, in cases it looks like it will use something real it would have to make a much more robust code. But also the whole solution is wrong, so it is not worth the effort to try to do right. This code will work well in most situations, but if an extraordinary situation happens, it will be a disaster.

If you later have to create other vectors of other types, it will complicate more.

Create a structure to keep the vector with its size:

typedef struct {
    size_t size;
    CACHE *vector;
} VectorCache

VectorCache *createCache(CACHEDESC desc) {
    VectorCache *vec = malloc(desc.number_of_lines * (sizeof CACHE));
    int associatividade = desc.associativity
    int contador = 0;
    for (int i = 0; i < desc.number_of_lines; i++) {
        CACHE auxiliar;
        auxiliar.tag = 0;
        auxiliar.index = contador;
        auxiliar.data = 0;
        auxiliar.time = clock();//start times
        memcpy(vec.vector[i], auxiliar, sizeof(auxiliar));
        --associatividade;
        if (associatividade == 0) {
            associatividade = desc.associativity;
            contador++;
        }
    }
    return vec;
}

void printaCache(VectorCache vec) {
    for (int i = 0; i < vec.size; i++) {
         printf("%d \t %d \t %d \t %d\n", i, vec.vector[i].tag, vec.vector[i].index, vec.vector[i].time);
    }
}

Printing time probably will not result in what you expect.

I hope you know where to put free() needed to have neither memory leak nor dangling pointer .

I would stress that this is the naive solution and that Anthony Accioly's comment is quite pertinent, so I'd rather use a library that has a similar structure to C ++% s.

    
18.11.2016 / 11:49