我在 initializeStruct 函数中遇到分段错误。我想要一个二维数组指针。每个二维数组索引都包含三种类型的结构。
这是结构:
struct cacheLine {
int validBit;
int tag;
int LRUcounter;
};
这是失败的方法:
void initializeStruct(struct cacheLine **anyCache){
int i, j;
for (i=0;i<S;i++){
for(j=0;j<E;j++){
anyCache[i][j].validBit = 0; //I am getting a Segmentation fault
anyCache[i][j].tag = 0;
anyCache[i][j].LRUcounter = 0;
}
}
return;
}
总的来说,我使用 malloc 来创建我的二维数组指针:
int main(int argc, char** argv){
int opt;
char *t;
//looping over arguments from command line
while(-1 != (opt = getopt(argc, argv, "s:E:b:t:"))){
//determine which argument it's processing
switch(opt){
case 's':
s = atoi(optarg);
break;
case 'E':
E = atoi(optarg);
break;
case 'b':
b = atoi(optarg);
break;
case 't':
t = optarg;
break;
//too many arguments
default:
printf("wrong argument\n");
break;
}
}
//create array
S = 1 << s;
B = 1 << b;
//allocate memory
struct cacheLine **cacheArray = malloc(sizeof(struct cacheLine)*S*E);
//Initialize Structs
initializeStruct(cacheArray);