title: using free for the host results in segmentation fault
body: <p>I have a cu file with the kernel like:</p>
  
  <pre><code>__global__ void kernel(float* A,float *B, curandState* globalState,int Asize,int Bsize)
  {
     ....
  
  }
  
  void kernel_wrapper(float** A_host,float** B_host, int Asize ,int Bsize)
  {
     ...
  
      //allocate host memory 
      *A_host=(float*)malloc(Asize*sizeof(float));
      *B_host=(float*)malloc(Bsize*sizeof(float));
  
      //allocate device memory
      float* A_dev,*B_dev;
      gpuErrchk(cudaMalloc((void**) &amp;A_dev,Asize* sizeof(float)));
      gpuErrchk(cudaMalloc((void**) &amp;B_dev,Bsize* sizeof(float)));
  
      // copy arrays from host to device
      gpuErrchk(cudaMemcpy(A_dev, *A_host,Asize* sizeof(float), cudaMemcpyHostToDevice));
      gpuErrchk(cudaMemcpy(B_dev, *B_host,Bsize* sizeof(float), cudaMemcpyHostToDevice));
  
      ....
      kernel&lt;&lt;&lt;dimGrid,dimBlock&gt;&gt;&gt;(A_dev,B_dev, devStates,Asize,Bsize);
  
      // copy result from device to host
      gpuErrchk(cudaMemcpy(*A_host, A_dev,Asize* sizeof(float), cudaMemcpyDeviceToHost));
      gpuErrchk(cudaMemcpy(*B_host, B_dev,Bsize* sizeof(float), cudaMemcpyDeviceToHost));
  
      //clean up device memory
      gpuErrchk(cudaFree(A_dev));
      gpuErrchk(cudaFree(B_dev));
      gpuErrchk(cudaFree(devStates));
      //clean up host memory
      free(*A_host);
      free(*B_host);
  }
  </code></pre>
  
  <p>and a cpp file from which I am calling the kernel:</p>
  
  <pre><code>...
  extern void kernel_wrapper(float** A,float** B, int Asize ,int Bsize);
  ...
  int main()
  {
      ...
      float* A;
      float* B;
      ...
      kernel_wrapper(&amp;A,&amp;B,Asize ,Bsize);
      ...
      free(A);
      free(B);
  </code></pre>
  
  <p>Now,using the  </p>
  
  <pre><code>free(*A_host);
  free(*B_host);
  </code></pre>
  
  <p>in cu file results in</p>
  
  <blockquote>
    <p>Segmentation fault</p>
  </blockquote>
  
  <p>If I use cudaFree or cudaFreeHost (which are not right because I am allocating with alloc) it shows "invalid device pointer" or "invalid argument".</p>
  
  <p>If I will not use free at all ,the program runs fine.</p>
  
  <p>Why is that and what is the appropriate process regarding these memory allocations?</p>