gpt4 book ai didi

c - 矩阵分配的段错误错误

转载 作者:行者123 更新时间:2023-11-30 17:34:43 25 4
gpt4 key购买 nike

这是我创建类似于游戏“地雷”的网格的代码:

#include <stdio.h>
#include <stdlib.h>

int main(){

int nr, nc, ** mtx, i, j;

//Matrix costruction

FILE * inStr;

inStr = fopen("brasa.dat","r");

if(inStr == NULL){

printf("\nProblem while opening the file.\n");

return 1;

}

fscanf(inStr, "%d%d", &nr, &nc);

mtx = (int**) malloc(nr * sizeof(int*));
if(mtx == NULL){
printf("\nMemory allocation error.\n");
return 1;
}
else{
for(i = 0; i < nr; i++)
mtx[i] = (int*) malloc(nc * sizeof(int));
if(mtx[i] == NULL){
printf("\nMemory allocation error.\n");
return 1;
}
}

//Filling matrix

for(i = 0; i < nr; i++){
for(j = 0; j < nc; j++)
mtx[i][j] = 0;
}

while(!feof(inStr)){
fscanf(inStr,"%d %d",&i,&j);
mtx[i][j] = 9;
}

fclose(inStr);

for(i = 0; i < nr; i++){
for(j = 0; j < nc; j++){

if(i == 0){
if(j == 0){
if(mtx[i][j+1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j+1] > 8)
mtx[i][j]++;
}
else if(j == nc-1){
if(mtx[i][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j] > 8)
mtx[i][j]++;
}
else{
if(mtx[i][j-1] > 8)
mtx[i][j]++;
else if(mtx[i][j+1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j+1] > 8)
mtx[i][j]++;
}
}
else if(i == nr-1){
if(j == 0){
if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j+1] > 8)
mtx[i][j]++;
else if(mtx[i][j+1] > 8)
mtx[i][j]++;
}
else if(j == nc-1){
if(mtx[i][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j] > 8)
mtx[i][j]++;

}
else{
if(mtx[i][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j+1] > 8)
mtx[i][j]++;
else if(mtx[i][j+1] > 8)
mtx[i][j]++;
}
}
else if(j == 0){
if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j+1] > 8)
mtx[i][j]++;
else if(mtx[i][j+1] > 8)
mtx[i][j]++;
else if(mtx[i-1][j+1] > 8)
mtx[i][j]++;
else if(mtx[i-1][j] > 8)
mtx[i][j]++;
}
else if(j == nc-1){
if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i][j-1] > 8)
mtx[i][j]++;
else if(mtx[i-1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i-1][j] > 8)
mtx[i][j]++;

}
else{
if(mtx[i-1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j-1] > 8)
mtx[i][j]++;
else if(mtx[i+1][j] > 8)
mtx[i][j]++;
else if(mtx[i+1][j+1] > 8)
mtx[i][j]++;
else if(mtx[i][j+1] > 8)
mtx[i][j]++;
else if(mtx[i-1][j+1] > 8)
mtx[i][j]++;
else if(mtx[i-1][j] > 8)
mtx[i][j]++;
}
if(mtx[i][j] > 8)
printf("*\t");
else
printf("%d\t", mtx[i][j]);
}
}

free(mtx);
return 0;
}

在程序执行过程中出现段错误。经过几次测试,我发现问题应该在第 85 行和第 158 行之间。你能帮我吗?

最佳答案

您的访问权限无效

        // ...
else if(i == nr-1){ // i is nr - 1
if(j == 0){
// ...
}
else if(j == nc-1){
// ...
else if(mtx[i+1][j] > 8) // i + 1 is nr (invalid access)
// ...

关于c - 矩阵分配的段错误错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com