title: Segmentation fault (core dumped) [Conway's game of life]
body: <p>I'm working on a C implementation for Conway's game of life, I have been asked to use the following header:</p>
  
  <pre><code>#ifndef game_of_life_h
  #define game_of_life_h
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  // a structure containing a square board for the game and its size
  typedef struct gol{
      int **board;
      size_t size;
  } gol;
  
  // dynamically creates a struct gol of size 20 and returns a pointer to it
  gol* create_default_gol();
  
  // creates dynamically a struct gol of a specified size and returns a pointer to it.
  gol* create_gol(size_t size);
  
  // destroy gol structures
  void destroy_gol(gol* g);
  
  // the board of 'g' is set to 'b'. You do not need to check if 'b' has a proper size and values
  void set_pattern(gol* g, int** b);
  
  // using rules of the game of life, the function sets next pattern to the g-&gt;board
  void next_pattern(gol* g);
  
  /* returns sum of all the neighbours of the cell g-&gt;board[i][j]. The function is an auxiliary 
  function and should be used in the following function. */
  int neighbour_sum(gol* g, int i, int j);
  
  // prints the current pattern of the g-board on the screen
  void print(gol* g);
  
  #endif
  </code></pre>
  
  <p>I have added the comments to help out with an explanation of what each bit is.</p>
  
  <p><code>gol.board</code> is a 2-level integer array, containing x and y coordinates, ie <code>board[x][y]</code>, each coordinate can either be a 1 (alive) or 0 (dead).</p>
  
  <p>This was all a bit of background information, I'm trying to write my first function <code>create_default_gol()</code> that will return a pointer to a <code>gol</code> instance, with a 20x20 board.</p>
  
  <p>I then attempt to go through each coordinate through the 20x20 board and set it to <code>0</code>, I am getting a <code>Segmentation fault (core dumped)</code> when running this program.</p>
  
  <p>The below code is my c file containing the core code, and the <code>main()</code> function:</p>
  
  <pre><code>#include "game_of_life.h"
  
  int main()
  {
      // Create a 20x20 game
      gol* g_temp = create_default_gol();
      int x,y;
      for (x = 0; x &lt; 20; x++)
      {
          for (y = 0; y &lt; 20; y++)
          {
              g_temp-&gt;board[x][y] = 0;
          }
      }
      free(g_temp);
  }
  
  // return a pointer to a 20x20 game of life
  gol* create_default_gol()
  {
      gol* g_rtn = malloc(sizeof(*g_rtn) + (sizeof(int) * 20 * 20));
      return g_rtn;
  }
  </code></pre>
  
  <p>This is the first feature I'd like to implement, being able to generate a 20x20 board with 0's (dead) state for every coordinate.</p>
  
  <p>Please feel free to criticise my code, I'm looking to determine why I'm getting the segmentation fault, and if I'm allocating memory properly in the <code>create_default_gol()</code> function.</p>
  
  <p>Thanks!</p>