0

Hi im having problems with my c code it keeps crashing with no error and im not sure why. i am trying to find the value at a point inside a 2d array for example [1][1] and see what the value is there (only 1 or a 0) and then process the value depending on if its 1 or a 0 but the program keeps crashing with no error and im not sure why.please help

typedef struct gol{ // structure containing a square board
    int **board; // pointer to a pointer
    size_t size; //size_t = unnasigned value
}gol;

the struct is created in main using

struct gol *GAME;
GAME = create_gol(30);

using an if menu options if option is selected it will just call next pattern function but it crashes

gol* create_gol(size_t size){

    struct gol *Game_Of_Life;
    Game_Of_Life = (struct gol*)malloc(sizeof(struct gol*)); //dynamically create the struct the ptr is pointing to **IMPORTANT
    Gameboard = new int*[size];

    for (int i = 0; i < size; ++i) {
        Gameboard[i] = new int[size];
        // each i-the pointer is now pointing to dynamic array (size 20) of actual int values
    }
    for (int i = 0; i < size; ++i) {   // for each row
        for (int j = 0; j < size; ++j) { // for each column
            Gameboard[i][j] = 0;
        }
    }
    Game_Of_Life->board=Gameboard;
    Game_Of_Life->size=size;

    return Game_Of_Life;
}


void next_pattern(gol* g)
{
    for (int i = 0; i < 20; ++i) {   // for each row
        for (int j = 0; j < 20; ++j) { // for each column
            int sum = neighbour_sum(g,i,j);
            if (g->board[i][j]==1){
                if (sum<2){
                    g->board[i][j]=0;
                }
                if (sum==3 || sum==2 ){
                    g->board[i][j]=1;
                }
                if (sum>3){
                    g->board[i][j]=0;
                }
            }
            if (g->board[i][j]==0 && sum==3){
                g->board[i][j]=1;
            }
        }
    }
}

updates neighbour sum so it cant go out of bounds program still crashing

 int neighbour_sum(gol* g, int i, int j)
 { int sum;
 if ((g->board[(i-1+g->size)%g->size][j])==1){ // left
 sum++;
 }
 if ((g->board[(i-1+g->size)%g->size][(j-1+g->size)%g->size])==1){//left up
 sum++;
  }
  if ((g->board[i][(j-1+g->size)%g->size])==1){ //up
 sum++;
  }
  if ((g->board[(i+1)%g->size][(j+1)%g->size])==1){ //right up
  sum++;
  }
  if ((g->board[i][(j+1)%g->size])==1){ //right
  sum++;
   }
  if ((g->board[(i+1)%g->size][(j+1)]%g->size)==1){//right bottom
   sum++;
  }
  if ((g->board[i][(j+1)%g->size])==1){//bottom
  sum++;
  }
  if ((g->board[(i-1+g->size)%g->size][(j+1)%g->size])==1){// bottom left
   sum++;
  }

  return sum;
   }
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Apr 23 '18 at 05:21
  • Please study and apply the concept of making a [mcve]. If you do that right, in 9 out of 10 cases, you will find the problem yourself. – Yunnosch Apr 23 '18 at 05:22
  • Welcome to StackOverflow. Please take the [tour] and read helpful things like [ask]. – Yunnosch Apr 23 '18 at 05:23
  • There should be even a very simple error msg, is there? – BartekPL Apr 23 '18 at 05:24
  • @BartekPL "keeps crashing with no error" seems to clearly contradict. You are still right though. Cranking up the compilers nit-pickiness should help. E.g. `gcc -Wall -pedantic`. – Yunnosch Apr 23 '18 at 05:25
  • a) You need to show us how you call the function. b) you need to show us how board is defined and initialized. c) seems strange that gol has a size member that isn't used – Support Ukraine Apr 23 '18 at 05:26
  • Most probably is that you read more than array have, you exceed the array size when indexing. This is often at start with C language. – BartekPL Apr 23 '18 at 05:28
  • `Gameboard = new int*[size];` ?? Are you compiling as C++ code? – Support Ukraine Apr 23 '18 at 05:44
  • `Gameboard = new int*[size];` What is `Gameboard`? Did you want: `Game_Of_Life->board = malloc(size * sizeof(int*));` – Support Ukraine Apr 23 '18 at 05:53
  • Welcome to Stack Overflow! Please [edit] your question to show us what kind of debugging you've done. I expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Apr 23 '18 at 14:12

2 Answers2

0

These lines

for (int i = 0; i < 20; ++i) {   // for each row
    for (int j = 0; j < 20; ++j) { // for each column
        int sum = neighbour_sum(g,i,j);

means that you first call neighbour_sum with both i and j being zero. Inside neighbour_sum you do:

if ((g->board[(i-1)][j])==1){ // left
               ^^^^

So since both i and j are zero, it is actually:

if ((g->board[-1][0])==1){ // left
             ^^^^
             ups 

So you access the array out of bounds. That may cause a crash. In any case it is illegal.

The general problem seems to be that you don't handle when the point is at the edge of the board.

edit after OP posted more code

This is wrong

Game_Of_Life = (struct gol*)malloc(sizeof(struct gol*));
                                                   ^^^

do

Game_Of_Life = malloc(sizeof(struct gol));
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • from what you said i tried starting the integers in the for loop at 1 rather then 0 this resulted in it no crashing when the array was empty (only 0 values ) when the array was populated with 1s it still crashes – Ben Leonforte Apr 23 '18 at 05:40
  • is there a way if it goes out of bounds of the array ie [-1][1] that i can use the value at the other end of the array [20][1] – Ben Leonforte Apr 23 '18 at 05:43
  • @BenLeonforte Well you could do something like: `[(size+i-1)%size]` to make sure the index is always in the range `0...(size-1)` – Support Ukraine Apr 23 '18 at 05:46
  • @BenLeonforte Seems you are mixing C and C++ code. `new` is not C code. In C you must use `malloc` – Support Ukraine Apr 23 '18 at 05:49
  • i have made all these changes but its still crashing – Ben Leonforte Apr 23 '18 at 06:31
  • @BenLeonforte Did you replace all `new` with `malloc`? – Support Ukraine Apr 23 '18 at 06:35
  • When trying to mallocc it gives error invalid conversion void* to int** – Ben Leonforte Apr 23 '18 at 06:48
  • @BenLeonforte That's because you are compiling the code as C++. Use a C compiler if you want to write C code. Example `gcc -Wall program.c` (if you are using gcc) – Support Ukraine Apr 23 '18 at 07:00
  • i didnt write the malloc function properly that error is fixed however program still crashing at same point – Ben Leonforte Apr 23 '18 at 07:25
  • @BenLeonforte Currently I don't know how your code looks so I can't tell where the bug is. I'll suggest that you do 1 of two things. Option 1) Post a new question with the code as it looks now. Option 2) Edit this question and add your current code **below** the original code (but don't edit the original code - leave that as it is). – Support Ukraine Apr 23 '18 at 07:35
0

I found the solution in the end there where a number of issues including i was looking for value outside of the array that was solved using

if ((g->board[(i-1+g->size)%g->size][j])==1){ // left
 sum++;
 }

i also was using c++ syntax instead of c syntax witch was resolved using the malloc function

Game_Of_Life = (struct gol*)malloc(sizeof(struct gol));

and the last issue causing the crashing still was the function neighbour sum was returning -2 because it wasn't initialized at 0 properly

int sum = 0;