C错误中的缓存模拟器
我已逐字复制了此代码并尝试运行它,但出现以下错误:C错误中的缓存模拟器,c,caching,C,Caching,我已逐字复制了此代码并尝试运行它,但出现以下错误: hw5.c:9:15:错误:应为声明说明符或数值常量前的“…” hw5.c:在函数“打印命中率”中: hw5.c:29:3:警告:格式为[-Wformat]的转换类型字符0xa未知 hw5.c:在函数“write_cache_table”中: hw5.c:59:10:错误:“cache_tag”未声明(此函数首次使用) hw5.c:59:10:注意:每个未声明的标识符对于它出现在其中的每个函数只报告一次 hw5.c:在函数“main”中: hw
hw5.c:9:15:错误:应为声明说明符或数值常量前的“…”
hw5.c:在函数“打印命中率”中:
hw5.c:29:3:警告:格式为[-Wformat]的转换类型字符0xa未知
hw5.c:在函数“write_cache_table”中:
hw5.c:59:10:错误:“cache_tag”未声明(此函数首次使用)
hw5.c:59:10:注意:每个未声明的标识符对于它出现在其中的每个函数只报告一次
hw5.c:在函数“main”中:
hw5.c:95:27:错误:赋值的左操作数需要左值
hw5.c:105:31:错误:“cache_tag”未声明(此函数首次使用)
hw5.c:116:28:错误:赋值1的左操作数需要左值
我会把全部代码都发出去。我看到有人问了一个关于第一个错误的类似问题,所以我采纳了把它放在我的main中的建议,但那没有起到任何作用
#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);
}
#包括
#包括
#定义要运行的流1//0,定义cache.out的流1
#定义主存储器1024
#定义缓存线10
#在每个缓存线中定义大小为8//8个WWORD的缓存块
int cache_标记(缓存线)//标签。如果cache_tag[i]=0,则表示未命中
int总内存访问量=0//内存访问量或地址请求
int total_hit=0//缓存命中数
//打印命中率
作废打印命中率(作废){
浮动比率;
如果(总命中率==0)
比率=0;
其他的
比率=(浮点)总命中/(浮点)总内存访问;
//获得百分比
比率*=100;
如果(总命中率==0)
比率=0;
printf(“命中率:%.2f%\n”,比率);
}
//打印缓存表的内容
void写缓存表(void){
int i,j;
FILE*ofp;//输出文件指针
如果(流)
ofp=fopen(“cache.out”,“w”);
其他的
ofp=stdout;
//打印表格标题
fprintf(ofp,“%6s |”,“行”);
对于(i=0;i
更改所有发生的
cache_tag(XX)
到
int cache_tag(cache_line);
绝对是无效的。哈哈,好主意!看视频时很难注意到区别。
cache_tag[XX]