Code:
if(cache[block].block == NULL){
cache[block].block = (int*)malloc(blockSize * sizeof(int));
}
This is the line that is having the error, but I am having a tough time figuring out how to allocate the memory correctly. I am trying to create a dynamic sized cache, based on user input and was told to put global variables in it even though I would have preferred not to. The majority of the code is below. It is a lot I know, I just don't know what I should be looking for.
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int tag;
int *block; //dynamic array
} *cache = NULL;
typedef struct node n;
int *mm = NULL;
int numLines, blockSize, cacheSize;
static void option1()
{
int mmSize, i, tag;
printf("Enter main memory size (words): ");
scanf("%d", &mmSize);
printf("Enter cache size (words): ");
scanf("%d", &cacheSize);
printf("Enter block size (words/blocks): ");
scanf("%d", &blockSize);
/*allocate space for MM and initialize*/
mm = (int *)malloc(mmSize * sizeof(int));
for (i = 0; i < mmSize; i++)
{
mm[i] = mmSize - 1;
}
numLines = cacheSize / blockSize;
/*allocate space for cache and initialize*/
cache = (n *)malloc(numLines * sizeof(n));
for (i = 0; i < cacheSize; i++)
{
cache[i].tag = -1;
cache[i].block = NULL;
}
tag = ((cacheSize/blockSize) - 1);
return;
}
static void option2()
{
int readOrWrite, mmAddress, writeValue, word, base, set, tag, block;
printf("Enter 0 for read or 1 for write: ");
scanf("%d", &readOrWrite);
if (readOrWrite == 1)
{
printf("\nEnter main memory address to write to: ");
scanf("%d", &mmAddress);
printf("\nEnter value to write: ");
scanf("%d", &writeValue);
}
else if (readOrWrite == 0)
{
printf("\nEnter main memory address to read from: ");
scanf("%d", &mmAddress);
}
/*calculate tag, line of cache, and word associated w/ MM address*/
printf("test1 \n");
tag = mmAddress / cacheSize;
block = 1;
set = (mmAddress % cacheSize) / blockSize;
word = mmAddress % blockSize;
base = (mmAddress / blockSize) * blockSize;
/*3 cases for hit/miss*/
//CASE 1: MISS DUE TO UNALLOCATED BLOCK
printf("test2 \n");
if (cache[block].block == NULL)
{
cache[block].block = malloc(blockSize * sizeof(cache[block].block[0]));
}
//CASE 2: MISS DUE TO NON MATCHING TAGS
if (cache[block].tag != tag)
{
cache[block].tag = tag;
int i;
for (i = 0; i < blockSize; i++)
{
cache[block].block[i] = mm[base + 1];
}
if (readOrWrite == 0)
{
printf("READ MISS!");
}
else if (readOrWrite == 1)
{
printf("WRITE MISS!");
mm[mmAddress] = writeValue;
}
}
//CASE 3: HIT IN THE CACHE, TAGS MATCH
else if (cache[block].tag == tag)
{
if (readOrWrite == 0)
{
printf("YOU HAVE A READ HIT!");
writeValue = cache[block].block[word];
}
else if (readOrWrite == 1)
{
printf("YOU HAVE A WRITE HIT!");
cache[block].block[word] = writeValue;
mm[mmAddress] = writeValue;
}
}
}
static void option4()
{
int i;
for (i = 0; i < numLines; i++)
{
if (cache[i].block != NULL)
{
free(cache[i].block);
}
if (cache != NULL)
{
free(cache);
}
}
}
int main()
{
int option = 0;
while (option != 4)
{
printf("\nCache memory allocation and mapping: \n");
printf("------------------------------------\n");
printf("1) Enter parameters \n");
printf("2) Access cache \n");
printf("3) Quit \n\n");
scanf("%d", &option);
switch (option)
{
case 1:
option1();
break;
case 2:
option2();
break;
case3:
option3();
break;
default:
printf("\nOption not available, please try again. \n");
break;
}
}
return 0;
}