简体   繁体   English

分段错误/ Dev C崩溃

[英]Segmentation fault/Dev C crashing

Hey guys so Im trying to do a homework and I cant find the fatal error on my program all day long.Let me explain : Firstly,you give the number of rows,col then the cells of the array (only "." for free spaces and "*" for mines, all in one row without spaces) then the crashing happens. 嘿,大家好,我正忙着做作业,我整日都找不到致命错误。让我解释一下:首先,您先给出行数,然后再输入数组的单元格(仅提供“。”)。空格和“ *”表示地雷,它们全部排成一行,没有空格),然后发生崩溃。

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);
}

Firstly, here's what I did to debug (actually I saw the problem in the code and just verified this way, but this will be useful to you). 首先,这是我所做的调试(实际上,我在代码中看到了问题,并以此方式进行了验证,但这对您很有用)。

  1. Add #include <stdio.h> and #include <stdlib.h> at the head of the file. 在文件的开头添加#include <stdio.h>#include <stdlib.h>

  2. gcc -Wall -O0 -g xc -ox to compile with debug and no optimisation. gcc -Wall -O0 -g xc -ox进行调试,并且不进行优化。

I then used following to run within gdb : 然后,我使用以下命令在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)

See how in less than 10 seconds it showed me where the error was? 看看在不到10秒的时间内如何显示错误所在?

You have two problems: 您有两个问题:

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

The SEGV appears here as you access p[N][M] , but the indices of p can only go from 0 to N-1 and 0 to M-1 respectively. 当您访问p[N][M]SEGV出现在此处,但是p的索引只能分别从0N-10M-1 This loop should probably read: 该循环可能显示为:

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

(note change to start at row=0 , and row < N not row <= M and similarly for col ). (请注意,更改从row=0开始,并且row < N而不是row <= M并且对于col类似)。

The second problem you have is to do with what to do at the edges: 您遇到的第二个问题是如何处理边缘:

Lines like this: 像这样的行:

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

should have the col > 1 condition first so they don't evaluate the array element unless the condition is true. 应具有col > 1 第一条件所以除非该条件为真,他们不评价数组元素。 Also, as col goes 0..M-1 , you want 另外,随着col变为0..M-1 ,您需要

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

Note I've put in some brackets to avoid any ambiguity. 请注意,为了避免歧义,我在括号中加上了一些内容。

The same applies when looking at the other edges: 在查看其他边缘时也是如此:

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

should be: 应该:

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

That should get you going. 那应该让你走。 But learn to use a debugger . 但是要学会使用调试器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM