我正在尝试让一个 C 程序在我的 mac 分区中工作,并且已经找到了大部分实现方法,但无法解决一个非常简单的问题。
所以我有一个 .h 文件,在其中声明所有变量,在 .c 文件中我尝试运行类似的命令
L2toL1 = (L2_transfer_time)*(L1_block_size);
但是我从来没有得到正确的答案。我在上面的行之前打印了变量,并且它们打印正确,但是当它们相乘时的答案就很遥远了。那么我怎样才能完成正确的乘法呢?
.h 文件看起来像
#define CONFIG_PARAM 16
#define HIT 1
#define MISS 0
#define TRUE 1
#define FALSE 0
unsigned long long L2=0;
unsigned long long L1=0;
int L1_block_size=0;
int L1_cache_size=0;
int L1_assoc=0;
int L1_hit_time=0;
int L1_miss_time=0;
int L2_block_size=0;
int L2_cache_size=0;
int L2_assoc=0;
int L2_hit_time=0;
extern int L2_miss_time=0;
int L2_transfer_time=0;
int L2_bus_width=0;
int mem_sendaddr=0;
int mem_ready=0;
int mem_chunktime=0;
int mem_chunksize=0;
unsigned long long test;
.c 文件然后运行以下命令并从配置文件中读取特定值 print f 语句的答案应该是 195,但实际上是 49567
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "definitions.h"
int main(int argc, char *argv[]) {
FILE *config_file, *trace;
unsigned int address, exec_info;
char check, trash[25], op;
int j, para;
int i=0;
if(argc >= 2)
config_file = fopen(argv[1],"r");
else
config_file = fopen("config0.txt","r");
// Grabs desired cache parameters from the specified config files
while(fscanf(config_file,"%s %d\n",trash,¶) == 2) {
config[i] = para;
i++;
}
// Close the config file
fclose(config_file);
// Puts Cache parameters from config file in desired variables
InitializeParameters();
/*
unsigned long long L2toL1;
L2toL1 = (L2_miss_time)*(L1_block_size);
printf("L2toL1 is: %Lu\n",L2toL1);
}
int InitializeParameters() {
L1_cache_size = config[0];
L1_block_size = config[1];
L1_assoc = config[2];
L1_hit_time = config[3];
L1_miss_time = config[4];
L2_block_size = config[5];
L2_cache_size = config[6];
L2_assoc = config[7];
L2_hit_time = config[8];
L2_miss_time = config[9];
L2_transfer_time = config[10];
L2_bus_width = config[11];
mem_sendaddr=config[12];
mem_ready=config[13];
mem_chunktime=config[14];
mem_chunksize=config[15];
}
感谢您的帮助
最佳答案
您可能会得到奇怪的数字,因为您的结果不适合目标变量。示例:您正在将两个数字相乘,但两个数字太大,以至于不适合目标内存位置。
例如,如果您有这段代码:
#include <stdio.h>
int main(int argc, char **argv)
{
int a, b, c;
a = 2000000000;
b = 2000000000;
c = a*b;
printf ("%d x %d = %d", a, b, c);
return 0;
}
将打印:
2000000000 x 2000000000 = -1651507200
话虽如此,在头文件中声明变量并不是一个好主意。
关于c - 在 mac os 上将两个变量名相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487013/