0

So I have been trying to solve this problem where I have to write a code that counts the number of mines that are around a certain point on a imaginary minefield. Here is the code:

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


int main(void) {
    int row;
    int col;
    int count;
    int mineCount = 0;
    int i;
    int j;

    // allocating memory

    scanf("%d %d", &row, &col);

    char **m = malloc(sizeof(char* ) * row);

    for(i = 0; i < row; i ++)
    {
        m[i] = malloc(sizeof(char) * (col + 1));        //empty minefield
    }

    //planting mines

    for(count = 0; count < row; count ++)
    {
        scanf("%s", m[count]);
    }

    // counting mines

    for(i = 0; i < row; i ++)
    {
        for(j = 0; j < (col + 1); j ++)
        {
            if(m[i][j] != 42)
                {
                    if(m[i-1][j] == 42)
                        mineCount += 1;
                    if(m[i+1][j] == 42)
                        mineCount += 1;
                    if(m[i][j+1] == 42)
                        mineCount += 1;
                    if(m[i][j-1] == 42)
                        mineCount += 1;
                    if(mineCount > 0)
                        m[i][j] = mineCount;
                }
        }
    }

    //printing out the minefield

    for(i = 0; i < row; i ++)
    {
        for(j = 0; j < col; j ++)
            printf("%c", m[i][j]);

        printf("\n");
    }


    return 0;
}

I put '5 5' for the first input, and for the minefield, my input would be something like this:

*....
..*..
...*..
.*...
....*

at the end though, I get this 'segmentation fault (Core dumped)' error. I have been searching around for answers and found out that this happens when I try to access something that I have no access to. Any help would be appreciated. Thanks.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Ryan
  • 1
  • 1
  • Plesse post also the stack trace so the error with the line it is appearing – Sebastian Walla May 11 '17 at 05:01
  • 1
    I would recommend compiling the code with the `-g` flag and then running the exucutable under a debugger like `gdb`. This will pinpoint what line is causing the segfault. – hugomg May 11 '17 at 05:01
  • 1
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh May 11 '17 at 05:02
  • `m[i-1][j]` if `i` is `0`, `m[-1][j]` – BLUEPIXY May 11 '17 at 05:03
  • As suggested, debugging will really help you know what is causing the problem. One concern I don't get is, if `mineCount` is not > 0, what do you do? If there are no surrounding mines? I feel you are not setting `m[i][j]` for every relevant value of i and j. Also, you are trying to access out of bounds arrays, `m[i+1][j]`, when i = row-1 is invalid – Stuti Rastogi May 11 '17 at 05:03
  • 1
    Your third input is 6 characters long `...*..` – Abhineet May 11 '17 at 05:04

3 Answers3

3
if(m[i-1][j] == 42)

if(m[i][j-1] == 42)

For these statements you need to have checks if i and j are not 0, otherwise you are accessing invalid memory

Pras
  • 4,047
  • 10
  • 20
1

Third input of ...*.. // 6 char long will make

//planting mines
for(count = 0; count < row; count ++)
{
    scanf("%s", m[count]);
}

to buffer overflow and write an extra . to out-of-bound memory.

Also, I don't see any free, assuming you must have implemented that.

Abhineet
  • 5,320
  • 1
  • 25
  • 43
0

Have a look at if(m[i+1][j]). If i=row-1 this will be equal to row. But as you only allocated row values and the addressing starts at 0 row-1 is the highest value, which you are able to address. And also at if (m[i-1][j]) and if(m[i][j-1]) when i or j is zero respectively.

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23