0
votes

I have this code, which is a function to make the steps for a percolation simulation in a bidimensional array.

int step (double ** mat, unsigned n, unsigned m, double a, double b)
{    
    int i, h, r, c, steps, x, y, o, v;                // search for minimum
    int min;
    FILE *fp;

    for(steps=0; steps<2; steps++)    // percolation steps
    { 
        for (o=0; o<n; o++)                                                              
        {
            for(v=0; v<m; v++)
            {
                if (mat[o][v]==-2) {mat[o][v]=-1;}
            }
        }               //trasformo i -2 in -1                                                                  

        min=b;                                 //set the minimum to max of range 
        for(x=0; x<n; x++)                    // i search in the matrix the invaded boxes 
        {
            for(y=0; y<m; y++)
            {
                if (mat[x][y]=-1)            //control for the boxes
                {                            
                    if (mat[x][y-1]<=min && mat[x][y-1]>=0) {min=mat[x][y-1]; r=x; c=y-1;}              //look for the minimum adjacent left and right
                    if (mat[x][y+1]<=min && mat[x][y+1]>=0) {min=mat[x][y+1]; r=x; c=y+1;}
                    for (i=-1; i<=1; i++)                                                                //look for the minimum adjacent up and down
                    {
                        for(h=-1; h<=1; h++)
                        {
                            if (mat[(x)+i][(y)+h]<=min && mat[(x)+i][(y)+h]>=0)
                            {
                                min=mat[(x)+i][(y)+h];
                                r=(x)+i;   c=(y)+h;
                            }    
                        }
                    }   
                }
            } 
        }   
        mat[r][c]=-2;   

        x=r; y=c;
    }   
    return 0;
}

When I use it in my main function I obtain Segmentation-fault (core dump created). Do you have any idea where the error is?

2
mat[x][y-1] with y = 0 leads to undefined behaviour. Same for y = m and mat[x][y+1]. Also, use of = instead of ==. Enable warnings in your compiler. - Zeta
Well, that's a spaghetti... - Eugene Sh.
BTW if (mat[x][y]=-1) ? - BLUEPIXY
@EugeneSh. ...western..? ;) - LPs
the cells already invaded are set to -1. - Francesco Ficara

2 Answers

0
votes

The segmentation fault (SF) is generated when you try to access a memory address that is not allocated to your program. There are some mistakes in your code

 if (mat[x][y+1]<=min && mat[x][y+1]>=0)

Here, the index will be out of range when y==m-1. This also applies to some other array index inside the loop

if (mat[x][y]=-1) 

Here is a typing error, the equal comparison operator should be ==.

It's hard to tell which part of your code is responsible for the SF. It will save you lots of time to use a debugger and catch the fault during runtime. You can then see the stack trace back and understand what happens.

0
votes

The segmentation fault is caused from the illegal memory address is trying to be accessed by your program.

I noticed that in the function you have two for loops,

                for (i=-1; i<=1; i++)                                                                //look for the minimum adjacent up and down
                {
                    for(h=-1; h<=1; h++)
                    {
                        if (mat[(x)+i][(y)+h]<=min && mat[(x)+i][(y)+h]>=0)
                        {
                            min=mat[(x)+i][(y)+h];
                            r=(x)+i;   c=(y)+h;
                        }    
                    }
                }

The variable 'i' & 'h' both start from -1, this will cause you to access the mat[-1][-1] in the beginning, which is not a legal memory address for the program to access.

You should re-design your loops to avoid exceeding the boundary of your array.