I am new to C.
I declared three structs
typedef struct{
int access_time;
int tag;
int valid;
}line;
typedef struct{
line *line;
int empty;
int insert;
}set;
typedef struct{
int set_num;
int line_num;
set *sets;
}cache;
cache* init(int s, int b, int E){
cache* c =malloc(sizeof(cache)) ;
assert(c!= NULL);
c->set_num = (1<<s);
c->line_num = E;
c->sets = malloc(c->set_num*sizeof(set));
for (int i=0;i<s;i++){
c->sets[i].empty=i;
c->sets[i].line=malloc(E*sizeof(line));
c->sets[i].insert=0;
}
return c;
}
The function is really simple. c.sets is an array and c->sets[i].line is also an array.
However, it seems that it does not create an array.Where did I wrong? I use xcode to debug this method, and c.sets is not an array, it only has on element, and c->sets[i].empty is always 0. If I use gcc -W -Wall -pedantic -std=c99 -g to compile, c->sets[i].empty updates. On Xcode, it does not. Thanks,
Sean
init()with? Ifsis 0 then the results you get are expected.c->set_numto1 << s, which is 2 raised to thesth power, and then allocating that many sets.