我已经逐字地复制了这个video的代码并试图运行它,但是我收到了以下错误:
hw5.c:9:15: error: expected declaration specifiers or '...' before numeric constant
hw5.c: In function 'print_hit_ratio':
hw5.c:29:3: warning: unknown conversion type character 0xa in format [-Wformat]
hw5.c: In function 'write_cache_table':
hw5.c:59:10: error: 'cache_tag' undeclared (first use in this function)
hw5.c:59:10: note: each undeclared identifier is reported only once for each function it appears in
hw5.c: In function 'main':
hw5.c:95:27: error: lvalue required as left operand of assignment
hw5.c:105:31: error: 'cache_tag' undeclared (first use in this function)
hw5.c:116:28: error: lvalue required as left operand of assignment1
我会发布整个代码。我看到有人问了一个关于第一个错误的类似问题,所以我接受了把它放在我的主要内部但没有做任何事情的建议。
#include <stdlib.h>
#include <stdio.h>
#define stream 1 //0 to run, 1 for cache.out
#define main_memory 1024
#define cache_line 10
#define cache_block_size 8 //8 wwords in each cache line
int cache_tag(cache_line); //tag. if cache_tag[i] = 0 its a MISS
int total_memory_access = 0; //amount of memory access or address request
int total_hit = 0; //number of cache hits
//print the hit ratio
void print_hit_ratio(void){
float ratio;
if (total_hit == 0)
ratio = 0;
else
ratio = (float)total_hit / (float)total_memory_access;
//get percentage
ratio *= 100;
if(total_hit == 0)
ratio = 0;
printf("Hit ratio:%.2f%\n", ratio);
}
//print contents of the cache table
void write_cache_table(void){
int i, j;
FILE *ofp; //output file pointer
if (stream)
ofp = fopen("cache.out", "w");
else
ofp = stdout;
//print table header
fprintf(ofp, "%6s|", "Lines ");
for (i = 0; i < cache_block_size; i++){
fprintf(ofp,"%6d|",i);
}
fprintf(ofp,"\n");
for (i = -1; i<cache_block_size; i++){
fprintf(ofp, "-------");
}
fprintf(ofp,"\n”);
//loop each each line
for(i = 0; i<cache_line;i++){
fprintf(ofp, "%6d|",i);
for(j=0;j<cache_block_size;j++){
//empty cache
if(cache_tag[i]==0)
fprintf(ofp,"%6d|",0);
else
fprintf(ofp,"%6d|",cache_tag[i]+j);
}
fprintf(ofp,"\n");
}
if(stream)
fclose(ofp);
}
int usage(void){
printf("Please pass a file\n");
printf("Usage: cachesim <file>\n");
return -1;
}
int main(int argc, char *argv[]){
FILE *ifp; //input file
int address_requested = 0; //address requested
if(argc != 2)
exit(usage());
//read input file from command line
ifp = fopen(argv[1], "r");
if(ifp == NULL) {
printf("File does not exist\n", argv[1]);
exit(usage());
}
//CODE BEGINS HERE
int i;
for (i=0;i<cache_line;i++){
cache_tag(cache_line) = 0;
}
int lastwrote = -1;
//read input file and store CPU request in address_requested
while(fscanf(ifp, "%d\n", &address_requested) != EOF){
int done = 0;
for(i = 0;i<cache_line;i++){
if(address_requested >= cache_tag[i] &&
(address_requested - cache_tag[i] < cache_block_size)){
total_hit++;
done = 1;
break;
}
}
if(done == 0){
lastwrote = (++lastwrote)%cache_line;
cache_tag(lastwrote) = address_requested;
}
total_memory_access++;
}
//END CODE
print_hit_ratio();
write_cache_table();
fclose(ifp);
}
答案 0 :(得分:2)
你困惑()和[]。改变所有出现的
cache_tag(XX)
到
cache_tag[XX]