title: Sending 2D Int Array with MPI_Send and Recv
body: <p>I am trying to send a 2D integer array of arbitrary length from slave processes to the master but I keep getting a segmentation fault. As MPI is quite difficult to debug, I'm not certain that the issue has to do with the send/recv but if it's not that then it will have to be with the way I am allocating the arrays themselves.</p>
  
  <p>I followed a previous question on here in regards to ensuring that the memory allocated to the array is contiguous but that still didn't fix the segmentation fault.</p>
  
  <p>Below are some sections of my code:</p>
  
  <p>Create array:</p>
  
  <pre><code>int** create2DArray(int sizeX, int sizeY)
  {
      int* data = (int *) malloc(sizeX * sizeY * sizeof(int));
      int** array= (int **) malloc(sizeX * sizeof(int*));
  
      int i;
  
      for (i=0; i&lt;sizeX; i++)
      {
          array[i] = &amp;(data[sizeY * i]);
      }
  
      return array;
  }
  </code></pre>
  
  <p>Initialise arrays:</p>
  
  <pre><code>if(rank==0)
  {
      display = x11setup(&amp;win, &amp;gc, width, height);
      pixels = create2DArray(X_RESN, Y_RESN);
  }
  else
  {
      xStart = xPixels * (rank - 1);
      xFinish = xStart + xPixels;
      pixels = create2DArray(xPixels, Y_RESN);
  } 
  </code></pre>
  
  <p>Send:</p>
  
  <pre><code>MPI_Send(&amp;pixels[0][0], xPixels * Y_RESN, MPI_INT, 0, type, MPI_COMM_WORLD);
  </code></pre>
  
  <p>Recv:</p>
  
  <pre><code>for(i = 1; i &lt; processes; i++)
  {
      int** pixelChunk = create2DArray(xPixels, Y_RESN);
      MPI_Recv(&amp;pixelChunk[0][0], xPixels * Y_RESN, MPI_INT, i, type, MPI_COMM_WORLD, &amp;status);
  
      int xStart = xPixels * (i - 1);
      int xFinish = xStart + xPixels;
  
      int k;
  
      for(j = xStart; j &lt; xFinish; j++)
      {
          for(k = 0; k &lt; Y_RESN; k++)
          {
              pixels[j][k] = pixelChunk[j - (xPixels * i - 1)][k];
          }               
      }
  }
  </code></pre>