4 Replies - 575 Views - Last Post: 29 June 2011 - 07:48 PM Rate Topic: -----

#1 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Segmentation Fault in my interpertation of Conway's Game of Life

Posted 29 June 2011 - 06:13 PM

Hi all,

So I am attempting an interpretation of Conway's game of life, and apparently I keep attempting to read/write out of bounds, but for the life of me I can't see the problem. Here is where the error occurs:

  while(true) { 
    for (n=1; n<=N; n++) {
      for (i=0; i<X; i++)
        for (j=0; j<Y; j++) {
          for (di=-1; di<=1; di++) 
            for (dj=-1; dj<=1; dj++) {
      
              //Somewhere in this loop

              s=0; if (di  ==0 &&   dj==0) {}
              else if (i+di <0 || j+dj <0) {}
              else if (i+di>=X || j+dj>=Y) {}
              else    s=b[i+di][j+dj]==0 ? s:s+1; 
            }



I wouldn't think it would be necessary, but here is the complete code for anyone interested:

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

#define false 0
#define true 1

int i,j,di,dj;
int X=37,Y=37;

void randb(int **B)/> {
  int r;
  for (i=0; i<X; i++)
    for (j=0; j<Y; j++) {
       r = rand_r((unsigned int *)time(NULL)) % 100 + 1;
       if (r < 10)  b[i][j]   = 1;
       else         b[i][j]   = 0;
    }
}


/* Check to see if any element of b 1 and b 2 is
 * non-equal; if so, return false; else return true. */
int beq(int **b1, int **b2) {
  for (i=0; i<X; i++)
    for (j=0; j<Y; j++)
       if(b1[i][j] != b2[i][j])
          return false;
  return true;
}
  
void pboard(int **B)/> {
  for (i=0; i<X; i++) {
    for (j=0; j<Y; j++) 
      printf(" %d", b[i][j]);   
    printf("\n");
  } 
}

int main() {
  int **b   = malloc(X*sizeof(int));
  for (i=0; i<X; i++) 
    b[i] = (int *) calloc(Y,sizeof(int));
  
    b[0][25] = b[1][23] = b[1][25] = b[5][23] = b[5][25] = b[8][14] = 
    b[2][13] = b[2][14] = b[2][21] = b[2][22] = b[2][35] = b[2][36] =
    b[3][12] = b[3][16] = b[3][21] = b[3][22] = b[3][35] = b[3][36] =
    b[4][1]  = b[4][2]  = b[4][11] = b[4][17] = b[4][21] = b[4][22] = 
    b[5][1]  = b[5][2]  = b[5][11] = b[5][17] = b[5][15] = b[5][18] = 
    b[6][11] = b[6][17] = b[6][25] = b[7][12] = b[7][16] = b[8][13] = 1;

  int n,s;
  int N = 10000;
  int **nb = malloc(X*sizeof(int));
  for (i=0; i<X; i++)
    nb[i]   = calloc(Y,sizeof(int));
  printf("After calloc of new board.\n");

  while(true) { 
    for (n=1; n<=N; n++) {
      for (i=0; i<X; i++)
        for (j=0; j<Y; j++) {
          for (di=-1; di<=1; di++) 
            for (dj=-1; dj<=1; dj++) {
              printf("i: %d; j: %d; di: %d; dj: %d\n", i, j, di, dj);
              s=0; if (di  ==0 &&   dj==0) {}
              else if (i+di <0 || j+dj <0) {}
              else if (i+di>=X || j+dj>=Y) {}
              else    s=b[i+di][j+dj]==0 ? s:s+1; 
            }
               if (b[i][j] >0 && s>=2) nb[i][j] = 1;
          else if (b[i][j]>=0 && s>=3) nb[i][j] = 1;
          else                         nb[i][j] = 0;
        } pboard(b=nb);
      printf("\nLast step: %d\n", n);
    }
  }
  return 0;
}




So.... any advice or anyone that sees something I don't see would be greatly appreciated. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation Fault in my interpertation of Conway's Game of Life

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Segmentation Fault in my interpertation of Conway's Game of Life

Posted 29 June 2011 - 06:39 PM

Something that caught my eye at a quick glance:

int **b   = malloc(X*sizeof(int));



int** points to pointers, should be:

int **b   = malloc(X*sizeof(int*));




Although on most systems both are 4 bytes, so you lucked out on that.


The seg fault is most likely coming from an array out of bounds access. Best bet is to run your code through a debugger and get the exact line throwing the seg fault and then we can proceed.
Was This Post Helpful? 2
  • +
  • -

#3 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Segmentation Fault in my interpertation of Conway's Game of Life

Posted 29 June 2011 - 06:50 PM

First off, thanks for the response, and I'll correct the int **b. I'll get on running through the debugger; but in the meantime I've inserted a print command at the top of that iterations:
            for (dj=-1; dj<=1; dj++) {
              printf("i: %d; j: %d; di: %d; dj: %d\n", i, j, di, dj);
              s=0; if (di  ==0 &&   dj==0) {}



With this being the tail end of the output before the fault:
i: 20; j: 0; di: -1; dj: 1
i: 20; j: 0; di: 0; dj: -1
i: 20; j: 0; di: 0; dj: 0
i: 20; j: 0; di: 0; dj: 1
i: 20; j: 0; di: 1; dj: -1
i: 20; j: 0; di: 1; dj: 0
i: 20; j: 0; di: 1; dj: 1
Segmentation fault (core dumped)


Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,735
  • Joined: 19-February 09

Re: Segmentation Fault in my interpertation of Conway's Game of Life

Posted 29 June 2011 - 07:36 PM

You could check whether the calls to calloc successfully allocate memory or not?
Was This Post Helpful? 1
  • +
  • -

#5 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Segmentation Fault in my interpertation of Conway's Game of Life

Posted 29 June 2011 - 07:48 PM

Found it. It was the same issue you noticed right off on int **b, just with int **nb. I was trying to overwrite a pointer with an int.
  int **nb = malloc(X*sizeof(int));



became:
  int **nb = malloc(X*sizeof(int*));



Thanks for your help! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1