2

嘿伙计们,所以我正在尝试做功课,我整天都找不到程序中的致命错误。让我解释一下:首先,你给出行数,然后是数组的单元格(只有“。”是免费的空格和“*”代表地雷,都在一行中,没有空格)然后发生崩溃。

main(){
    int  i,col,row,count,N,M,j;
    char **p;
    printf("Give number of rows\n");
    scanf("%d",&N);
    printf("Give number of columns\n");
    scanf("%d\n",&M);
    p=malloc(N*sizeof(char *));   //Saving room for the array
    if (p==NULL)
        return -1;
    for (i=0;i < N ; ++i){
        p[i] = malloc (M * sizeof(char));
        if (*(p+i) == NULL)
            return -1;
    }
    for (i=0; i< N;++i){
        for ( j = 0 ; j < M ;++j)
            scanf("%c",&p[i][j]); //Insert "*" for mines and the rest with "."
    }
    for (row=1; row<= N;++row){           //Here the things get messy
                for ( col = 1 ; col <= M ;++col){
                    if(p[row][col]=='.'){
                        count = 0 ;
                        if(p[row][col+1]=='*' && col < M)
                            count=count+1;
                        if(p[row][col-1]=='*' && col > 1)
                           count=count+1;
                        if(p[row+1][col]=='*' && row < N)
                            count=count+1;
                        if(p[row-1][col]=='*' && row > 1)
                            count=count+1;
                        if(p[row+1][col+1]=='*' && (row < N && col < M))
                            count=count+1;
                        if(p[row+1][col-1]=='*' && (row < N && col > 1))
                            count=count+1;
                        if(p[row-1][col+1]=='*' && ( row > 1 && col < M))
                            count=count+1;
                        if(p[row-1][col-1]=='*' && ( row > 1 && col > 1))
                            count=count+1;
                        printf("%d ", count);
                    }
                    printf("* ");           
                }
                printf("\n");
    }
    printf("\n");
     for (i=0; i< N;++i){       
                for ( j = 0 ; j < M ;++j)
                        printf("%c ",p[i][j]);
        printf("\n");
     }
    for (i = 0 ; i <N ; ++i)
        free(p[i]);
    free(p);
}
4

1 回答 1

5

首先,这是我调试时所做的(实际上我在代码中看到了问题并只是通过这种方式进行了验证,但这对您很有用)。

  1. 在文件的开头添加#include <stdio.h>和。#include <stdlib.h>

  2. gcc -Wall -O0 -g x.c -o x用调试和无优化编译。

然后我使用以下方法在内部运行gdb

gdb x
...
(gdb) run
Starting program: /home/amb/so/x
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Give number of rows
1
Give number of columns
1

.

Program received signal SIGSEGV, Segmentation fault.
0x00000000004007d4 in main () at x.c:25
25                      if(p[row][col]=='.'){
(gdb) print row
$1 = 1
(gdb) print col
$2 = 1
(gdb)

看看它如何在不到 10 秒的时间内告诉我错误在哪里?

你有两个问题:

for (row=1; row<= N;++row){           //Here the things get messy
            for ( col = 1 ; col <= M ;++col){
                if(p[row][col]=='.'){

SEGV您访问 时, 会出现在此处p[N][M],但 的索引p只能分别从0toN-10to转到M-1。这个循环可能应该是:

for (row=0; row < N;++row){           //Here the things get messy
            for ( col = 0 ; col < M ;++col){
                if(p[row][col]=='.'){

(注意更改为从 开始row=0,而row < N不是从 开始,row <= M类似地col)。

您遇到的第二个问题是在边缘做什么:

像这样的行:

if (p[row][col-1]=='*' && col > 1)
   count=count+1;

应该首先col > 1具有条件,因此除非条件为真,否则他们不会评估数组元素。另外,你想要col0..M-1

if ((col > 0) && (p[row][col-1]=='*'))
    count=count+1;

请注意,为了避免歧义,我在括号中加上了一些括号。

在查看其他边缘时也是如此:

if (p[row][col+1]=='*' && col < M)
    count=count+1;

应该:

if ((col < M-1) && (p[row][col+1]=='*'))
    count=count+1;

那应该让你继续前进。但要学会使用调试器

于 2014-03-09T18:03:40.110 回答