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