title: Simple CUDA Test always fails with "an illegal memory access was encountered" error
body: <p>If I run this program I get &quot;an illegal memory access was encountered in matrixMulti.cu at line 48&quot; error. I searched and tried a lot. So I hope somebody can help me.</p>
  <blockquote>
  <p>Line 48 :  HANDLE_ERROR ( cudaMemcpy(array, devarray, N<em>N</em>sizeof(int), cudaMemcpyDeviceToHost) );</p>
  </blockquote>
  <p>The program is just to get into CUDA. I tried to implement a matrix multiplication.</p>
  <pre><code>#include &lt;iostream&gt;
  #include&lt;cuda.h&gt;
  #include &lt;stdio.h&gt;
  
  using namespace std;
  
  #define HANDLE_ERROR( err ) ( HandleError( err, __FILE__, __LINE__ ) )
  void printVec(int** a, int n);
  
  static void HandleError( cudaError_t err, const char *file, int line )
  {
      if (err != cudaSuccess)
      {
      printf( &quot;%s in %s at line %d\n&quot;, cudaGetErrorString( err ),
              file, line );
      exit( EXIT_FAILURE );
      }
  }
  
  void checkCUDAError(const char *msg)
  {
      cudaError_t err = cudaGetLastError();
      if( cudaSuccess != err) 
      {
          fprintf(stderr, &quot;Cuda error: %s: %s.\n&quot;, msg, 
                                cudaGetErrorString( err) );
          exit(EXIT_FAILURE);
      }                         
  }
  __global__ void MatrixMulti(int** a, int** b) {
      b[0][0]=4;
  }
  
  int main() {
      int N =10;
      int** array, **devarray;
      array = new int*[N];
  
      for(int i = 0; i &lt; N; i++) {
          array[i] = new int[N];  
      }
      
      HANDLE_ERROR ( cudaMalloc((void**)&amp;devarray, N*N*sizeof(int) ) );
      HANDLE_ERROR ( cudaMemcpy(devarray, array, N*N*sizeof(int), cudaMemcpyHostToDevice) );  
      MatrixMulti&lt;&lt;&lt;1,1&gt;&gt;&gt;(array,devarray);
      HANDLE_ERROR ( cudaMemcpy(array, devarray, N*N*sizeof(int), cudaMemcpyDeviceToHost) );
      HANDLE_ERROR ( cudaFree(devarray) );
      printVec(array,N);
  
      return 0;
  }
  
  void printVec(int** a , int n) {
      for(int i =0 ; i &lt; n; i++) {
          for ( int j = 0; j &lt;n; j++) {
          cout&lt;&lt; a[i][j] &lt;&lt;&quot; &quot;;
          }       
          cout&lt;&lt;&quot; &quot;&lt;&lt;endl;    
      }
  }
  </code></pre>