views:

93

answers:

2

I am trying to build this project and for some reason the program hangs when I run it. It works fine if i comment out the data cache lines. but it cannot make a call to makeCache for two different caches i dont know why any C experts know. Im new to c.

   /*
 * main.c
 *
 *  Created on: Sep 16, 2010
 *      Author: TJ
 */
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int tag;
int valid;
int LRU;
int offset;
}directoryBlock;

typedef struct{
int setNumber;
directoryBlock blocks[];
}cacheSet;

typedef struct{
int cacheNumber;
cacheSet *sets[];
}cache;

cache* makeCache(cache *makeMe,int numberOfSets, int blocksPerSet);


int main(void)
{
    int i = 0;
    //cache * temp = makeCache(10,200);
    i = 0;
    int j = 0;
    cache *instructions = malloc(sizeof(cache) + sizeof(cacheSet*));
    cache *data = malloc(sizeof(cache) + sizeof(cacheSet*));
    makeCache(instructions,20,300);
    makeCache(data,20,300);
    for(j=0;j < 20;j++)
            {
                for(i = 0; i < 300;i++)
                {
                    printf("Data From Set %d Block %d, Valid %d, Tag %d, LRU %d, Offset %d\n",j,i
                            ,instructions->sets[j]->blocks[i].valid,instructions->sets[j]->blocks[i].tag
                            ,instructions->sets[j]->blocks[i].LRU,instructions->sets[j]->blocks[i].offset);
                }
            }

    return 0;

}

cache* makeCache(cache *makeMe,int numberOfSets,int blocksPerSet)
{
    int i = 0;
    int j = 0;
    for(j=0; j < numberOfSets;j++)
    {

        cacheSet *newSet = malloc(sizeof(cacheSet) + sizeof(directoryBlock)*blocksPerSet);
        for(i = 0; i < blocksPerSet; i++)
        {
            directoryBlock temp;
            temp.LRU = i*j;
            temp.tag = i*j;
            temp.offset = i*j;
            temp.valid = i;
            newSet->blocks[i] = temp;
        }
        makeMe->sets[j] = newSet;
    }



    return makeMe;
}
+3  A: 

You're not allocating space for the cacheSet array, you have 20 cacheSets so try this with the "20 *" added to your lines:

cache *instructions = malloc(sizeof(cache) + 20 *sizeof(cacheSet*));
cache *data = malloc(sizeof(cache) + 20 * sizeof(cacheSet*));
Graham Perks
A: 

In your main function you're allocating the memory for your cache. Since you have a function dedicated to creating the cache, it should allocate the memory. That function has the parameters to determine the total memory required. If you do it separately, you're repeating that information and require yourself to remember exactly how to allocate the memory. If the function makeCache does it for you, it will save pain later. Just be sure your documentation notes that the makeCache function allocates memory.

Your cache memory is incorrect, but not as pointed out before. Just sizeof(cache) is the right size. This will make room for the int and the cacheSet **. You should then allocate memory for the cacheSet array in each cache. Then you should allocate memory for each directoryBlock in each cacheSet in the cache.... kittens, cats, sacks, and wives...

So all your allocations should just be Thing *t sizof(Thing);

In psuedocode:

cache *c = malloc(sizeof(cache))
for 0 to number of cacheSets:
  cacheSet *s = malloc(sizeof(cacheSet))
  for 0 to number of blocks:
    block *b = malloc(sizeof(block))
    //fill in data

JD

JoshD