简体   繁体   中英

Implementing Caches in C

I am confused and not sure if what i am doing is correct...

typedef struct Node {
   size_t tag;                
   struct Node *next;   
   int valid;
} Node;

typedef struct caches {
   struct Node *tag; 
   int hits;
   int misses;
   int Coldmisses;
} caches;

These are my structs.....

I created a method...

caches* L1Cache() {
    caches *l1 = malloc(numberofsets1*sizeof(caches));
    if (numberofsets1 != 1) {
        numberofsets1 = L1Size/(blocksize*L1assoc);
    }
    l1->hits = 0;
    l1->misses = 0;
    l1->tag->valid = 0;
    l1->Coldmisses = 0;
    return l1;
}

in my main I called it like caches* L1 = L1Cache() ; then can i use the L1 something like this???? Also I am getting SEGEMENTATION FAULT "l1->tag->valid = 0;" here IDK WHY?? Help please

if (L1[indexoffset].tag->valid = 0) {
    L1[indexoffset].tag->tag = tag;
    L1[indexoffset].tag->valid = 1;
}

What if I add l1->tag=malloc(sizeof(Node)); in caches* L1Cache() right after l1->misses = 0; – user3100209

It should work. Do not forget to free it at the end or if you change the pointer ( l1->tag=othernode ). – francis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM