I have a C program with the following structs and main function.
Code:
struct Block {
int tag;
int valid;
};
struct Set {
struct Block *blocks;
int mru;
};
struct Cache {
int BlockSize, NumSets, NumWays; // block size, number of sets, associativity
struct Set *sets;
};
struct Cache *cache_init(const char *name, const char *config)
{
struct Cache *c = (struct cache_t *)malloc(sizeof(struct cache_t));
if (sscanf(config, "%u:%u:%u", &c->bsize, &c->nsets, &c->nways) != 3) {
fprintf(stderr, "%s configuration: none|<bsize>:<nsets>:<nways>\n", name);
exit(-1); }
else if (c->BlockSize < 1 || !IS_POWEROF2(c->BlockSize)) {
fprintf(stderr, "%s bsize (%u) must be positive and a power of 2\n",name,
c->BlockSize); exit(-1);
}
else if (c->NumSets < 1 || !IS_POWEROF2(c->NumSets)) {
fprintf(stderr, "%s sets (%u) must be positive and a power of 2\n", name,
c->NumSets);
exit(-1);
}
else if (c->NumWays < 1) {
fprintf(stderr, "%s ways (%u) must be positive\n", c->Numways);
exit(-1);
}
return c;
}
int main(int argc, char **argv) {
char buf[256];
FILE *f = fopen("house.trace", "r");
struct Cache *InstructionCache = NULL;
struct Cache *DataCache = NULL;
// check if program was called with the correct number of arguments
// if not, print usage message and exit
if (argc != 3) {
fprintf(stderr, "usage: MyProgram none|<i-arg1>:<i-arg2>:<i-arg3>
none|i|<d-arg1>:<d-arg2>:<d-arg3>\n");
exit(-1);
}
printf("argv[0] = %s\n", argv[0]) ;
printf("argv[1] = %s\n", argv[1]) ;
printf("argv[2] = %s\n", argv[2]) ;
printf("strcmp(argv[1], none) = %d", strcmp(argv[1], "none")) ;
if (strcmp(argv[1], "none") == 0) {
icache = cache_init("icache", argv[1]);
}
else {
InstructionCache = cache_init("icache", argv[1]) ;
DataCache = cache_init("dcache", argv[2]) ;
}
/* More code */
}
The program gives a Bus error (core dumped) at the line:
if (sscanf(config, "%u:%u:%u", &c->bsize, &c->nsets, &c->nways) != 3)
Does anyone know how to fix this?
I thank the solver in advance.