Im writing this program for a class assignment in C. Its to simulate reading and writing to a custom-sized direct-mapped cache, involving a custom-sized main memory.
These are the parameters I used before getting Segmentation fault:
Enter main memory size (words):  65536
Enter cache size (words):  1024
Enter block size (words/block):  16
This is my code. Its not complete yet.
#include<stdio.h>
#include<stdlib.h>
struct cache{
     int tag;
     int *block;
};
struct main {
     int value;
     int address;
     int word;
     struct main *next;
};
struct cache   *cacheptr;
struct main    *mainHd;
int main() {
    int option = 0;
    while (option != 4) {
        printf("Main memory to Cache memory mapping:\n--------------------------------------");
        printf("\n1) Set parameters\n2) Read cache\n3) Write to cache\n4) Exit\n\nEnter Selection: ");
        scanf("%d",&option);
        printf("\n");
        if (option == 1) {
            int mainMemory, cacheSize, block;
            printf("Enter main memory size (words):  ");
            scanf("%d",&mainMemory);
            printf("Enter cache size (words):  ");
            scanf("%d",&cacheSize);
            printf("Enter block size (words/block):  ");
            scanf("%d",&block);
            struct main *mainptr=(struct main *)malloc(cacheSize);
            mainHd->next=mainptr;
            for (int i=1; i < mainMemory; i++) {
                mainptr->value=mainMemory-i;
                mainptr->address=i;
                mainptr->word=i;
                struct main *mainnxt=(struct main *)malloc(cacheSize);
                mainptr->next=mainnxt;
                mainptr=mainnxt;
            }
        } /* end if */
    } /* end while */
} /* end main */
 
     
    